1) It does require RTTI. At least if you are using it on polymorphic classes...which is really its purpose.
2) If you don't have RTTI on the dynamic_cast can't check to see if the object you are casting is actually an object type you are casting to. This is the difference between dynamic_cast (essentially) and static_cast. Static_cast does not check this, and thus is less 'safe' but faster. Thus if you have no RTTI, it CAN'T do a dynamic cast when you're downcasting
Upcasting is safe because you can determine types at compile time, and thus you can upcast with dynamic_cast to the base class.
3) dynamic_cast downcasting is for polymorhpic types (eg types with a virtual function in them), and thus if the class isn't polymorphic its not going to work. So if CBase doesn't have a virtual function you can't use dynamic_cast with it.
Same reason as 2 for the upcasting.
I think you should read a bit on what the different casts are for.
发布评论
评论(1)
1)它确实需要RTTI。至少如果您在多态类上使用它......这确实是它的目的。
2) 如果您在dynamic_cast 上没有RTTI,则无法检查您正在投射的对象是否实际上是您正在投射到的对象类型。这就是dynamic_cast(本质上)和static_cast之间的区别。 Static_cast 不检查这一点,因此不太“安全”,但速度更快。因此,如果您没有 RTTI,则在向下转换时它无法执行动态转换
向上转换是安全的,因为您可以在编译时确定类型,因此您可以使用dynamic_cast向上转换到基类。
3)dynamic_cast 向下转型适用于多态类型(例如,其中带有虚函数的类型),因此如果该类不是多态的,则它不会工作。因此,如果 CBase 没有虚拟函数,则无法使用dynamic_cast。
与 2 向上转换的原因相同。
我认为你应该了解一下不同演员的用途。
http://www.cplusplus.com/doc/tutorial/typecasting/
4 )是的,它会将其编译为内联。
1) It does require RTTI. At least if you are using it on polymorphic classes...which is really its purpose.
2) If you don't have RTTI on the dynamic_cast can't check to see if the object you are casting is actually an object type you are casting to. This is the difference between dynamic_cast (essentially) and static_cast. Static_cast does not check this, and thus is less 'safe' but faster. Thus if you have no RTTI, it CAN'T do a dynamic cast when you're downcasting
Upcasting is safe because you can determine types at compile time, and thus you can upcast with dynamic_cast to the base class.
3) dynamic_cast downcasting is for polymorhpic types (eg types with a virtual function in them), and thus if the class isn't polymorphic its not going to work. So if CBase doesn't have a virtual function you can't use dynamic_cast with it.
Same reason as 2 for the upcasting.
I think you should read a bit on what the different casts are for.
http://www.cplusplus.com/doc/tutorial/typecasting/
4) Yes it will compile it as inline.