这是一段代码:
有这样一段代码:
#include <iostream>
class Base
{
int x;
};
class Derived : virtual public Base
{
int y;
};
int main()
{
std::cout << sizeof(Derived) << std::endl; // prints 12
return 0;
}
我了解到,当某个类进行虚拟继承时,会为派生类创建一个空的虚函数表,因此内存布局如下:
Derived::ptr to empty vtable
Derived::y
Base::x
它有12个字节。问题是 - 如果没有任何虚拟方法,这个空的 vtable的目的是什么,以及它如何被使用?