同C#一样, 不定期更新, C/C++笔记
1 .类静态成员变量的使用注意事项
class A {
public:
static int N;
// other members or methods
A() {
N = 0; // right
}
};
int A::N; // 这步是必须的, 这是对类静态成员的定义, 若没有定义, 将会出现令人头疼的晦涩的链接错误, 因为N虽然声明但未定义 , 这样导致编译通过但无法链接成可执行程序(, 大概是因为找不到A::N这个内存单元, 因为A::N未分配)
int main() {
A a = new A();
a.N = 1; // right, but not recommanded
A::N = 2; // right, recommand, because N is belonged to A not A's instance
return 0;
}
可以尝试把上面定义的那部分注释看能否执行此程序