Does dynamic_cast behave as a static_cast, if there is not a single virtual function?
In a c开发者_如何学编程lass hierarchy without any virtual functions, will dynamic_cast
behave as a simple static_cast
since it doesn't have any information stored for RTTI, or it will give an error?
It's easy to check:
class A {};
class B : public A {};
int main(int argc, char **argv) {
A* a = new B();
B* b = dynamic_cast<B*>(a);
}
G++ says:
error: cannot dynamic_cast 'a' (of type 'class A*') to type 'class B*' (source type is not polymorphic)
BTW for this kind of questions I find online llvm-gcc demo useful.
精彩评论