有虚函数的字节对齐问题 困惑??
class Base
{
public:
char c3;
double d;
char c4;
Base(){}
virtual ~Base(){}
};
void main()
{
cout << "alignment of Base = " << __alignof(Base) << endl;
cout << "offset of Base.c3 = " << offsetof(Base, c3) << endl;
cout << "offset of Base.d = " << offsetof(Base, d) << endl;
cout << "offset of Base.c4 = " << offsetof(Base, c4) << endl;
cout << "sizeof(Base) = " << sizeof(Base) << endl;
return;
}
为什么c3的偏移是8而不是4呢?Base类的开始部分应该是vptr,其大小应该是四个字节,这样的话c3原本的偏移应该是0 + 4 = 4,而c3的alignment应该是1,为什么还要对齐到8呢?困惑啊