struct A{ A() {} ~A() {} int m1; int m2; }; struct B:A{ B() {} ~B() {} int m1; char m2; static char m3; }; struct C{ C() {} virtual~C() {} int m1; short m2; };
#include <iostream> using namespace std; struct A { A() { } ~A() { } int m1; int m2; }; struct B :A { B() { } ~B() { } int m1; char m2; static char m3; }; struct C { C() { } virtual~C() { } int m1; short m2; }; int main() { cout << "sizeof(A): " << sizeof(A) << endl; cout << "sizeof(B): " << sizeof(B) << endl; cout << "sizeof(C): " << sizeof(C) << endl; cin.get(); return 0; }
类B继承类A,在类A的8个字节基础上,再加上B自己的大小8字节,所以类B大小是16个字节;
类C是4个字节+4个字节(考虑对齐)+4个字节(指向虚析构函数的指针)=12个字节