C++ 初始化类变量的正确位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/722822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:56:38  来源:igfitidea点击:

Correct place to initialize class variables?

c++

提问by Tony R

Where is the correct place to initialize a class data member? I have the class declaration in a header file like this:

初始化类数据成员的正确位置在哪里?我在头文件中有类声明,如下所示:

Foo.h:

福.h:

class Foo {
private:
    int myInt;
};

Then I try to set a value to myInt in the corresponding .cpp file:

然后我尝试在相应的 .cpp 文件中为 myInt 设置一个值:

Foo.cpp:

文件.cpp:

int Foo::myInt = 1;

I get a compiler error for redefining myInt. What am I doing wrong???

重新定义 myInt 时出现编译器错误。我究竟做错了什么???

回答by Eclipse

What you have there is an instance variable. Each instance of the class gets its own copy of myInt. The place to initialize those is in a constructor:

你有一个实例变量。类的每个实例都有自己的 myInt 副本。初始化它们的地方是在构造函数中:

class Foo {
private:
    int myInt;
public:
    Foo() : myInt(1) {}
};

A class variable is one where there is only one copy that is shared by every instance of the class. Those can be initialized as you tried. (See JaredPar's answer for the syntax)

类变量是只有一个副本被类的每个实例共享的变量。这些可以在您尝试时初始化。(有关语法,请参阅 JaredPar 的答案)

For integral values, you also have the option of initializing a static const right in the class definition:

对于整数值,您还可以选择在类定义中初始化静态常量:

class Foo {
private:
    static const int myInt = 1;
};

This is a single value shared by all instances of the class that cannot be changed.

这是无法更改的类的所有实例共享的单个值。

回答by GManNickG

To extend on Jared's answer, if you want to initialize it the way it is now, you need to put it in the Constructor.

为了扩展 Jared 的答案,如果您想按照现在的方式对其进行初始化,则需要将其放入构造函数中。

class Foo
{
public:
    Foo(void) :
    myInt(1) // directly construct myInt with 1.
    {
    }

    // works but not preferred:
    /*
    Foo(void)
    {
        myInt = 1; // not preferred because myInt is default constructed then assigned
                   // but with POD types this makes little difference. for consistency
                   // however, it's best to put it in the initializer list, as above
                   // Edit, from comment: Also, for const variables and references,
                   // they must be directly constructed with a valid value, so they
                   // must be put in the initializer list.
    }
    */

private:
    int myInt;
};

回答by user1914692

It can be initialized directly in the header file, in c++11 or gnu++11:

可以直接在头文件中初始化,在c++11或gnu++11中:

int myInt = 1;

See this article "C++11 Tidbits: Non-static Data Member Initializers"

参见这篇文章“ C++11 花絮:非静态数据成员初始化器

回答by JaredPar

You're attempting to initialize an instance member via a static initialization construct. If you want this to be a class level variable (static) then precede the variable with the static keyword.

您正在尝试通过静态初始化构造初始化实例成员。如果您希望这是一个类级变量(静态),则在变量前加上 static 关键字。

class Foo {
private:
  static int myInt;
};

回答by Fabio Vinicius Binder

A class variable must be marked as "static". If your variable is an instance variable and not a class variable you must initialize it in the constructor or other method.

类变量必须标记为“静态”。如果变量是实例变量而不是类变量,则必须在构造函数或其他方法中对其进行初始化。