虚拟继承和内存布局


GCC/32Bit architecture  
class B
{
int x,y;
};
class D1: public virtual B
{
int z;
};
class D2; public virtual B
{
int z;
public:
virtual void func(){}
};
int main() {
B b; D1 d1; D2 d2;
cout<<sizeof(b)<<endl;
cout<<sizeof(d1)<<endl;
cout<<sizeof(d2)<<endl;
return 0;
}  

根据我的理解, B 有 2 个整数:8 字节 D1: offet to B(B_vPtr(, x,y,z => 16. D2: B_vBase, VPTR, x, y => 16

Ans i am getting is 8, 24, 24.
What is the size of the class and how memory allocated for this classes. 
How is Vptr and Vtable managed in these cases.

你用的是什么编译器?视觉工作室?海湾合作委员会?铛?什么目标架构(主要是 32 位或 64 位(? 您还可以查看虚拟继承情况下类的大小。

我不是在看布局,但我想说的是,指向父级的指针与其实际数据之间存在填充,以使数据具有 8 字节对齐。这意味着 4 字节 + 填充(4( + 8 字节的父数据 + 本地数据 + 填充?

最新更新