[英]Why should I initialize static class variables in C++?
In C and C++ all static variables are initialized by default to ZERO. 在C和C ++中,所有静态变量默认初始化为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;
}
At class scope, 在课堂范围内,
int MyClass::classVar = 0; // Why I have to init it here?
is a definition and 是一个定义和
static int classVar;
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 请注意,根据值初始化规则,您可以相处
int MyClass::classVar; // Zero-initialized !
as a definition. 作为一个定义。
Variables declared at namespace scope are definitions too (unless they are extern
qualified): 在命名空间范围内声明的变量也是定义(除非它们是extern
限定的):
int var;
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 is const
, it becomes automatically static
and there is no violation of the One Definition Rule should it be put into a multiply included header. [注意,在C ++中(而不是在C中),如果上面的var
是const
,它将自动变为static
并且如果将其放入包含多个的头中,则不会违反单定义规则。 This goes slightly off topic, but feel free to ask details] 这有点偏离主题,但随意询问细节]
C++ FAQ 10.12 states that: C ++ FAQ 10.12声明:
static data members must be explicitly defined in exactly one compilation unit. 必须在一个编译单元中显式定义静态数据成员。
From C++ FAQ http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12 来自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? 这是回答你的问题,还是在你提到C ++标准之后?
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, for double
0.0, for a string
"" etc. In contrast, classes do not necessarily have a default value. 变量类型具有逻辑“零值”,对于int
它为0,对于double
0.0,对于string
“”等。相反,类不一定具有默认值。 Consider, for example class Rectangle
. 考虑一下,例如class 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. 对于静态变量,编译器会要求您自己定义静态变量必须具有的值,因为并非每种数据类型都可以通过默认值进行初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.