FAQ: Why does dynamic_cast only work if a class has at least 1 virtual method?

后端 未结 3 2021
挽巷
挽巷 2020-11-28 11:36

This does not compile in C++:

class A
{
};

class B : public A
{
};

...

A *a = new B();
B *b = dynamic_cast(a);
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 11:42

    As the other stated: The standard says so.

    So why does the standard says so?

    Because if the type isn't polymorphic it may (or is? Question to the standard gurus) be a plain type. And for plain types there are many assumptions coming from the C backwards compatibility. One of those is that the type only consists of it's members as the developer declared + necessary alignment bytes. So there cannot be any extra (hidden) fields. So there is no way to store in the memory space conserved by A the information that it really is a B.

    This is only possible when it is polymorphic as then it is allowed to add such hidden stuff. (In most implementations this is done via the vtable).

提交回复
热议问题