开发者

Does dynamic_cast behave as a static_cast, if there is not a single virtual function?

开发者 https://www.devze.com 2023-02-07 07:53 出处:网络
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?I

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

精彩评论

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