#include#include struct A{ }; class B{ virtual void func(){ ; } }; class C:public B{ }; int main(int argc,char** argv) { using namespace std; A a; B b; C c; cout << sizeof(A) << endl; cout << sizeof(B) << endl; cout << sizeof(C) << endl; cout << sizeof(a) << endl; cout << sizeof(b) << endl; cout << sizeof(c) << endl; }
1
8
8
1
8
8
如果一个类有虚函数,那么就有虚函数表,那么如果这个类不包含任何成员变量,则它的大小为一个指针的空间。
如果一个类没有虚函数,那么就没有虚函数表,那么如果这个类不包含任何成员变量,则编译器会给它分配一个字节的默认大小。


![[c/c++] 类和结构体占用空间问题 [c/c++] 类和结构体占用空间问题](http://www.mshxw.com/aiimages/31/938474.png)
