gol*_*ean 1 c++ variables static class
对不起,如果这个问题对很多人来说似乎微不足道.
在C++代码中,有如下内容:
class Foo
{
public:
static int bands;
...
...
private:
...
...
}//class definition ends
int Foo::bands; //Note: here its not initialized to any value!
Run Code Online (Sandbox Code Playgroud)
当'bands'一次在类中声明为static时,为什么上述语句需要再次使用?
静态变量也可以在任何类中声明为私有成员变量吗?
C++注意到声明和定义之间的区别. bands在类中声明,但未定义.
当您定义该类型的对象时,将定义非静态数据成员,但由于静态成员不是任何一个特定对象的一部分,因此它需要它自己的定义.