The reason why dynamic_cast doesn't work with non-polymorphic types

老子叫甜甜 提交于 2019-12-10 14:17:39

问题


With classes B and derived class D:

class B {
    int b;
};


class D : public B {
    int d;
};


D* d = new D();
B* b = dynamic_cast<B*>(d);

the above will work fine—it's a simple upcasting. We're sure that whatever b is pointing at has the B class (sub-)object in it.

However,

B* b = new D();
D* d = dynamic_cast<D*>(b);

won't compile even though b points to a valid D instance—because the base class is not polymorphic. So adding just one, empty virtual method would solve the problem.

The important question is why C++ requires the source type to be polymorphic? The only explanation I've found is this, but it merely states 'because that's how it's implemented internally' - at least in my eyes). People who designed dynamic_cast probably had some other reasons in mind - what were those?


回答1:


Because there is no way to implement dynamic_cast without some type information stored in the object for run-time use. And there are only two features of the language which need a run-time information on the object's type: virtual functions, and dynamic_cast.

If it was possible to use dynamic_cast to downcast non-polymorphic types, compliers would have to store run-time type information in every class type. This would go directly against the "only pay for what you use" philosophy of C++, and it would utterly break its compatibility with C and many external interfaces, hardware etc. There would be no standard-layout class types, basically. Or, alternatively, no class types where you could have full control of their layout.




回答2:


The premise of dynamic_cast is that it uses RTTI (presumably every implementation uses the same underlying data structure to support dynamic_cast and RTTI - the type info has to live somewhere and different representations for the two use cases makes no sense) to make sure that the cast you're attempting is to the correct type at runtime. If the from-class is non-polymorphic, the type information wouldn't be available to the compiler to do the type checking (to decided to return 0 or the converted pointer).



来源:https://stackoverflow.com/questions/35729540/the-reason-why-dynamic-cast-doesnt-work-with-non-polymorphic-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!