Do C++ POD types have RTTI?

懵懂的女人 提交于 2019-12-06 03:15:56

问题


As I understand how RTTI is implemented in various C++ compilers (such as GCC), a pointer to the type_info data is stored in the vtable data of each class.

And also as mentioned here, POD type may not have a vtable.

But if POD types may not have a vtable then where is the pointer to the type_info stored? I know it is implementation-specific, but it would be better to be aware of a C++ compiler (such as GCC) internals.


回答1:


There are two kinds of types (for the purposes of RTTI): polymorphic types and non-polymorphic types. A polymorphic type is a type that has a virtual function, in itself or inherited from a base class. A non-polymorphic type is everything else; this includes POD types, but it includes many other types too.

If you have a pointer/reference to a polymorphic type T, and you call typeid on it, the type_info you get back is not necessarily the type_info you would get back for typeid(T{}). Instead, it is the true dynamic type of the object: the most derived class.

If you have a pointer/reference to a non-polymorphic type T, typeid will always return the type_info for T itself. Non-polymorphic types always assume that the pointer/reference is exactly its static type.

POD types are non-polymorphic, but a huge number of other types are non-polymorphic as well.



来源:https://stackoverflow.com/questions/35342258/do-c-pod-types-have-rtti

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