据我所知,C ++中类的大小取决于以下因素:
现在,我创建了两个类,如下所示:
class A{
int a;
short s;
int b;
char d;
};// kept a char at last on purpose to leave a "hole"
class B : public A{
char c;
};
Run Code Online (Sandbox Code Playgroud)
现在检查A和BI的大小,请参见
我的假设是B类中的字符c被容纳在A类中剩余的“空洞”中。
但是,令我感到困惑的是以下情况,其中我将成员公开
class A{
public:
int a;
short d;
int b;
char s;
};
class B : public A{
public:
char c;
};
Run Code Online (Sandbox Code Playgroud)
现在大小变成
我似乎无法理解这种差异的原因。