虚拟继承情况下的 vtable [英] vtable in case of virtual inheritance
问题描述
在虚拟继承的情况下,vtable 有什么用(或者为什么需要 vtable)?在这种情况下,这个 vtable 指向什么.
What is the use of vtable (or why is vtable required ) in case of virtual inheritance ? what does this vtable points to in this case.
示例:
class A
{
void show()
{ }
};
class B : virtual A
{
void disp()
{ }
};
在上面的例子中,B 类的大小是 8 个字节.这意味着 B 类的 vptr 指向一个 Vtable.这个 vtable 指向什么.
In the above example the size of class B is 8 bytes. which means class B has vptr pointing to a Vtable. What does this vtable point to .
推荐答案
vtable 是在 C++ 中实现 virtual
关键字的最常见方式——任何使用 virtual
关键字将为它创建一个 vtable,并且该类的每个实例都将包含一个指向该(单个)vtable 的指针.vtable 包含有关对象动态类的信息(以支持 dynamic_cast
和 typeinfo
)以及有关该类的虚拟基类和函数所在位置的信息.
A vtable is the most common way of implementing the virtual
keyword in C++ -- any class that uses the virtual
keyword will have a vtable created for it and every instance of that class will contain a pointer to that (single) vtable. The vtable contains information on the dynamic class of the object (to support dynamic_cast
and typeinfo
) as well as information as to where virtual base classes and functions of the class are located.
在这种特定情况下,B
的 vtable 可能只包含动态类信息,因为 A
没有数据成员或虚函数.
In this specific case, the vtable for B
will likely contain just dynamic class info, as A
has no data members or virtual functions.
这篇关于虚拟继承情况下的 vtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!