为什么要在 C++ 中初始化静态类变量?

发布于 2024-12-18 03:49:59 字数 540 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最佳男配角 2024-12-25 03:49:59

在类范围内,

int MyClass::classVar = 0;  // Why I have to init it here?

是一个定义,并且

static int classVar;

是一个声明,即。承诺变量将在某处定义:您必须恰好定义一次您声明的变量。

理由是类声明可能会包含在多个源文件中。如果它的一部分是定义,它将发生乘法:这是错误的(例外是内联[成员]函数)。

注意,根据值初始化规则,您可以将 和

int MyClass::classVar;  // Zero-initialized !

作为定义。

在命名空间范围内声明的变量也是定义(除非它们是 extern 限定的):

int var;

是一个声明,也是一个定义:如果将其放入标头并将其包含在多个翻译单元中,则会出现错误(“多重定义的符号”,或类似的东西)。

[注意,在 C++ 中(而不是 C),如果上面的 varconst,它会自动变为 static 并且有如果将其放入多重包含的标头中,则不会违反单一定义规则。这有点偏离主题,但请随意询问详细信息]

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):

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. This goes slightly off topic, but feel free to ask details]

超可爱的懒熊 2024-12-25 03:49:59

C++ FAQ 10.12 指出:

静态数据成员必须在一个编译单元中显式定义。

来自 C++ 常见问题解答 http://www.parashift.com/c++- faq-lite/ctors.html#faq-10.12

这是否回答了您的问题,或者您是否在参考 C++ 标准本身?

C++ FAQ 10.12 states that:

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

Does that answer your question or were you after a reference to the C++ standard itself?

瞄了个咪的 2024-12-25 03:49:59

您必须初始化静态类数据变量,因为您必须告诉编译器它们的值是什么。类不需要有默认值的概念。

变量类型有一个逻辑“零值”,对于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, for double 0.0, for a string "" etc. In contrast, classes do not necessarily have a default value. Consider, for example 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文