为什么要在 C++ 中初始化静态类变量?
在 C 和 C++ 中,所有静态变量都默认初始化为零。
这不是静态类数据成员的情况。这是为什么?
#include <iostream>
using namespace std;
int var;
class MyClass
{
public:
static int classVar;
};
int MyClass::classVar = 0; // Why I have to init it here?
int main(void)
{
cout << ::var << endl; // this is initalized to ZERO by default
static int var;
cout << var << endl; // and this also is initalized to Zero
cout << MyClass::classVar << endl;
return 0;
}
In C and C++ all static variables are initialized by default to ZERO.
This is not the case of static class data members. Why is that?
#include <iostream>
using namespace std;
int var;
class MyClass
{
public:
static int classVar;
};
int MyClass::classVar = 0; // Why I have to init it here?
int main(void)
{
cout << ::var << endl; // this is initalized to ZERO by default
static int var;
cout << var << endl; // and this also is initalized to Zero
cout << MyClass::classVar << endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在类范围内,
是一个定义,并且
是一个声明,即。承诺变量将在某处定义:您必须恰好定义一次您声明的变量。
理由是类声明可能会包含在多个源文件中。如果它的一部分是定义,它将发生乘法:这是错误的(例外是内联[成员]函数)。
注意,根据值初始化规则,您可以将 和
作为定义。
在命名空间范围内声明的变量也是定义(除非它们是
extern
限定的):是一个声明,也是一个定义:如果将其放入标头并将其包含在多个翻译单元中,则会出现错误(“多重定义的符号”,或类似的东西)。
[注意,在 C++ 中(而不是 C),如果上面的
var
是const
,它会自动变为static
并且有如果将其放入多重包含的标头中,则不会违反单一定义规则。这有点偏离主题,但请随意询问详细信息]At class scope,
is a definition and
is a declaration, ie. a promise the variable will be defined somewhere: you must define exactly once the variables you declare.
The rationale is that the class declaration will likely be included in multiple source files. Would a part of it be a definition, it would take place multiply: this is erroneous (exceptions are inline [member] functions).
Note that according to value initialization rules, you can get along with
as a definition.
Variables declared at namespace scope are definitions too (unless they are
extern
qualified):is a declaration, and a definition: if you put this into a header and include it in multiple translation units, you have an error ("multiply defined symbol", or something along those lines).
[Note that in C++ (and not in C), if the
var
above isconst
, it becomes automaticallystatic
and there is no violation of the One Definition Rule should it be put into a multiply included header. This goes slightly off topic, but feel free to ask details]C++ FAQ 10.12 指出:
来自 C++ 常见问题解答 http://www.parashift.com/c++- faq-lite/ctors.html#faq-10.12
这是否回答了您的问题,或者您是否在参考 C++ 标准本身?
C++ FAQ 10.12 states that:
From C++ FAQ http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12
Does that answer your question or were you after a reference to the C++ standard itself?
您必须初始化静态类数据变量,因为您必须告诉编译器它们的值是什么。类不需要有默认值的概念。
变量类型有一个逻辑“零值”,对于
int
来说是0,对于double
来说是0.0,对于string
来说是“”等。相反,类不一定有默认值。例如,考虑类 Rectangle
。它的零值是多少——正方形为零的矩形还是边长为单位的矩形?对于静态变量,编译器要求您自己定义静态变量必须具有什么值,因为并非每种数据类型都可以用默认值初始化。You have to initialize your static class data variables, because you have to tell the compiler what their value is. Classes need not have a notion of a default value.
Variables types have a logical "zero value", for
int
it is 0, fordouble
0.0, for astring
"" etc. In contrast, classes do not necessarily have a default value. Consider, for exampleclass Rectangle
. What is its zero value - a rectangle with zero square or a rectangle with unit side length? For static variables, a compiler asks you to define yourself, what value your static variable must have, because not every data type can be initialized by a default value.