Do C ++ POD类型有RTTI吗? [英] Do C++ POD types have RTTI?
问题描述
由于我理解RTTI是如何在各种C ++编译器(如GCC)中实现的,指向 type_info
数据的指针存储在
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.
此处,POD类型可能没有 vtable
。
And also as mentioned here, POD type may not have a vtable
.
但是如果POD类型可能没有 vtable
,那么指向 type_info
的指针是否存储?我知道它是实现特定的,但最好是注意一个C ++编译器(如GCC)内部。
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.
推荐答案
p>有两种类型(为了RTTI的目的):多态类型和非多态类型。多态类型是一种具有虚函数的类型,本身或继承自基类。非多态类型是其他;
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.
如果你有一个指向/指向一个多态类型 T
,你调用 typeid
就可以了,你回来的 type_info
不一定是 type_info
,您将返回 typeid(T {})
。相反,它是对象的 true 动态类型:最导出的类。
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.
如果你有一个指针/引用< >非 - 多态类型 T
, typeid
将始终 code> $ $
type_info
。非多态类型总是假定指针/引用是它的静态类型。
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类型是非多态的,但是大量的类型是非多态的
POD types are non-polymorphic, but a huge number of types are non-polymorphic as well.
这篇关于Do C ++ POD类型有RTTI吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!