1.常见类型实例大小(32位环境下)
空类的实例:1字节,理由:任意实例在内存中都有唯一的地址,为此,编译器给空类的实例加一个字节空间。
enum:4字节。
2.字节对齐(略)
3.同类型不同平台的差异(略)
4.virtual
注意:本次是32位环境下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> using namespace std; #pragma pack(4) class Base { public: virtual void func(){} }; class Child001 : public Base { public: int m_a; char m_b; }; class Child002 { public: int m_a; char m_b; public: virtual void func(){} }; class Child003 { public: int m_a; char m_b; public: virtual void func(){} virtual void func2(){} }; class Base002 { public: virtual void func(){} virtual void func1(){} }; class Child004 : public Base002 { public: int m_a; char m_b; public: virtual void func(){} virtual void func2(){} virtual void func3(){} }; class Child005 : public Base002 { public: char m_a; public: virtual void func(){} virtual void func1(){} public: char m_b; }; class Child006 : public Base002 { public: char m_c; char m_d; public: virtual void func(){} virtual void func2(){} }; class Child007 { public: virtual void func(){} }; class Child008 : public Base002 { public: char m_c; char m_d; void *m_e; public: virtual void func(){} virtual void func2(){} }; class Child009 : public Base002 { public: char m_c; char m_d; void *m_e; }; class Child010 { public: char m_c; char m_d; void *m_e; }; class Child011 { public: char m_c; char m_d; }; struct Child012 { char m_c; char m_d; }; class Base003 { public: }; class Child013 : virtual public Base003 { public: }; class Base004 { public: virtual void func(){} }; class Child014 : virtual public Base004 { public: }; class Base005 { }; class Child015 : public Base005 { }; class Base006 { public: void func(){} }; class Child016 : virtual public Base006 { }; class Base007 { public: virtual void func(){} }; class Base007_2 { public: virtual void func(){} }; class Child017 : public Base007, public Base004, public Base007_2 {}; class Child018 : virtual public Base007, virtual public Base004, virtual public Base007_2 {}; int main(int, char**) { cout << sizeof(Child001) << endl;//output 12 cout << sizeof(Child002) << endl;//output 12 cout << sizeof(Child003) << endl;//output 12 cout << sizeof(Child004) << endl;//output 12 cout << sizeof(Child005) << endl;//output 8 cout << sizeof(Child006) << endl;//output 8 cout << sizeof(Child007) << endl;//output 4 cout << sizeof(Child008) << endl;//output 12 cout << sizeof(Child009) << endl;//output 12 cout << sizeof(Child010) << endl;//output 8 cout << sizeof(Child011) << endl;//output 2 cout << sizeof(Child012) << endl;//output 2 cout << sizeof(Child013) << endl;//output 4 cout << sizeof(Child014) << endl;//output 8 cout << sizeof(Child015) << endl;//output 1 cout << sizeof(Child016) << endl;//output 4 cout << sizeof(Child017) << endl;//output 12 cout << sizeof(Child018) << endl;//output 16 system("pause"); return 0; }
理由:32位环境下,m_a占4字节,m_b的数据占用了1字节,但由于字节对齐,m_b的字段共占4字节(补齐4,m_c和m_d同理),本身存储了一个指向自身虚函数表的指针占用了4字节(该指针不会随着虚函数的增加而增加),从例子中,虚函数表的指针不需要考虑声明顺序的问题。
空类继承空类,子类:1字节
空类虚继承空类,子类:4字节(子类带虚函数表指针)
空类虚继承带方法(非虚函数)的类,子类:4字节(子类带虚函数表指针)
空类虚继承带虚函数的类,子类:8字节(子类和父类带虚函数表指针)
总结:
每继承一个带虚函数表的空基类就累加4字节。
虚继承让派生类(即使是空类)带虚函数表于是产生4字节,而普通继承时派生类不带虚函数则不会有虚函数表,即不会产生4字节。
兰叶凛香原创博文:http://www.cnblogs.com/xiaobalang/