c++中类对象分配内存大小与虚继承的一些问题

来源:互联网 发布:mac系统下怎么截图 编辑:程序博客网 时间:2024/05/14 08:32
#include <iostream>
class Base
{
public:
int a;

};


class Derive1 : public virtual Base
{
public:
int c;
};

class Derive2 : virtual public Base
{

};


class d_Derive : public virtual Derive1, public virtual Derive2
{
};

int main(void)
{
Base bb;
Derive1 d1;
Derive2 d2;
d_Derive dd;
std::cout << sizeof(bb) << std::endl
<< sizeof(d1) << std::endl 
<< sizeof(d2) << std::endl
<< sizeof(dd) << std::endl;
return 0;

}

运行结果为:



我的困惑:

1、为什么d1的大小为12

2、为什么d2的大小为8

3、为什么dd的大小为20,不是说虚继承共享虚基类的子对象吗?

。。。。。。还在探索中

0 0