类中成员变量和成员函数分开存储 1 #include2 using namespace std; 3 class person 4 { 5 6 }; 7 8 void test() 9 {10 person p;11 // 空对象占一个字节为什么呢?12 // C++为每个空对象分配一个字节的内存空间,是为区分空对象占内存的位置 13 cout << "sizeof of null obj is " << sizeof(p) << endl;14 }15 16 int main()17 {18 test();19 return 0;20 }
1 #include2 using namespace std; 3 class person 4 { 5 int age;//非静态成员变量属于类对象上的 ,剩下都不是 6 static int va;//静态成员变量不属于类对象上的 7 void func(){ 8 } 9 static void fun(){10 } 11 };12 13 void test()14 {15 person p;16 // 空对象占一个字节为什么呢?17 // C++为每个空对象分配一个字节的内存空间,是为区分空对象占内存的位置 18 cout << "sizeof of null obj is " << sizeof(p) << endl;19 }20 21 void test01()22 {23 person p;24 cout << "sizeof of null obj is " << sizeof(p) << endl;25 } 26 27 int main()28 {29 test01();30 return 0;31 }