#include<iostream>
using namespace std;
class grandparent
{
public:
int x;
};
class parent1 : virtual public grandparent
{
public:
int y;
};
class parent2 : virtual public grandparent
{
public:
int z;
};
class child : public parent1, public parent2
{
};
int main()
{
child obj;
cout<<sizeof(obj)<<endl;
}
输出:20
#include<iostream>
using namespace std;
class grandparent
{
public:
int x;
};
class parent1 : public grandparent
{
public:
int y;
};
class parent2 : public grandparent
{
public:
int z;
};
class child : public parent1, public parent2
{
};
int main()
{
child obj;
cout<<sizeof(obj)<<endl;
}
输出:16
当parent1和parent2继承而没有虚拟时,我得到的子对象大小是16?但是当parent1和parent2继承虚拟时,为什么我得到了20岁而不是12岁的大小