开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜