#include <iostream>
class Base
{
public:
int a;
{
public:
int c;
};
class Derive2 : virtual public Base
{
{
};
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;
class Base
{
public:
int a;
};
{
public:
int c;
};
class Derive2 : virtual public Base
{
};
{
};
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,不是说虚继承共享虚基类的子对象吗?
。。。。。。还在探索中
本文通过一个C++示例程序探讨了虚继承机制下派生类的内存布局情况。主要分析了不同继承方式对类实例大小的影响,并针对具体运行结果进行了解释。

被折叠的 条评论
为什么被折叠?



