28 lines
444 B
C++
28 lines
444 B
C++
|
// 静态成员变量不占类对象的空间
|
||
|
#include <iostream>
|
||
|
#include <cstring>
|
||
|
#include <cstdlib>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
class A
|
||
|
{
|
||
|
public:
|
||
|
int m;
|
||
|
static int x;
|
||
|
A(int m)
|
||
|
{
|
||
|
this->m = m;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// int A::x = 20; // 静态变量初始化
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
A a1(3);
|
||
|
cout << "A size " << sizeof(A) << endl; // 同下
|
||
|
cout << "a1 size " << sizeof(a1) << endl; // 4 Byte , 为 int m 的大小
|
||
|
return 0;
|
||
|
}
|