C++:在构造函数中初始化变量的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6822422/
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-28 20:43:33  来源:igfitidea点击:

C++: Where to initialize variables in constructor

c++constructorinitialization

提问by Greg Howell

Possible Duplicate:
C++ initialization lists

可能的重复:
C++ 初始化列表

What are the pros/cons of initializing variables at option 1 vs option 2?

在选项 1 和选项 2 中初始化变量的优缺点是什么?

class MyClass
{
public:
    MyClass( float f, char a );
private:
    float mFloat;
    char mCharacter;
    bool mBoolean;
    int mInteger;
};

MyClass::MyClass( float f, char a ) : mFloat( f ), mBoolean( true ) // option 1.
{
    // option 2
    mCharacter = a;
    mInteger = 0;
}

Edit: Why is option 2 so common?

编辑:为什么选项 2 如此普遍?

回答by Greg Howell

In short, always prefer initialization lists when possible. 2 reasons:

简而言之,在可能的情况下总是更喜欢初始化列表。2个原因:

  • If you do not mention a variable in a class's initialization list, the constructor will default initialize it before entering the body of the constructor you've written. This means that option 2 will lead to each variable being written to twice, once for the default initialization and once for the assignment in the constructor body.

  • Also, as mentioned by mwigdahl and avada in other answers, const members and reference members can onlybe initialized in an initialization list.

  • 如果你没有在类的初始化列表中提到一个变量,构造函数将在进入你编写的构造函数的主体之前默认初始化它。这意味着选项 2 将导致每个变量被写入两次,一次用于默认初始化,一次用于构造函数体中的赋值。

  • 此外,正如 mwigdahl 和 avada 在其他答案中所提到的,const 成员和引用成员只能在初始化列表中进行初始化。

Also note that variables are always initialized on the order they are declared in the class declaration, not in the order they are listed in an initialization list (with proper warnings enabled a compiler will warn you if a list is written out of order). Similarly, destructors will call member destructors in the opposite order, last to first in the class declaration, after the code in your class's destructor has executed.

还要注意,变量总是按照它们在类声明中声明的顺序进行初始化,而不是按照它们在初始化列表中列出的顺序进行初始化(如果启用了适当的警告,编译器会在列表写错时发出警告)。类似地,在类的析构函数中的代码执行后,析构函数将以相反的顺序调用成员析构函数,在类声明中从最后到第一个。

回答by mwigdahl

Although it doesn't apply to this specific example, Option 1 allows you to initialize member variables of reference type (or consttype, as pointed out below). Option 2 doesn't. In general, Option 1 is the more powerful approach.

尽管它不适用于此特定示例,但选项 1 允许您初始化引用类型(或const类型,如下所述)的成员变量。选项 2 没有。一般来说,选项 1 是更强大的方法。

回答by Rapha?l Saint-Pierre

See Should my constructors use "initialization lists" or "assignment"?

请参阅我的构造函数应该使用“初始化列表”还是“赋值”?

Briefly: in your specific case, it does not change anything. But:

简而言之:在您的特定情况下,它不会改变任何东西。但:

  • for class/struct members with constructors, it may be more efficient to use option 1.
  • only option 1 allows you to initialize reference members.
  • only option 1 allows you to initialize const members
  • only option 1 allows you to initialize base classes using their constructor
  • only option 2 allows you to initialize array or structs that do not have a constructor.
  • 对于具有构造函数的类/结构成员,使用选项 1 可能更有效。
  • 只有选项 1 允许您初始化引用成员。
  • 只有选项 1 允许您初始化 const 成员
  • 只有选项 1 允许您使用其构造函数初始化基类
  • 只有选项 2 允许您初始化没有构造函数的数组或结构。

My guess for why option 2 is more common is that option 1 is not well-known, neither are its advantages. Option 2's syntax feels more natural to the new C++ programmer.

我对为什么选项 2 更常见的猜测是选项 1 并不为人所知,它的优势也不为人所知。对于新的 C++ 程序员来说,选项 2 的语法感觉更自然。

回答by Diego Sevilla

Option 1 allows you to use a place specified exactly for explicitly initializing member variables.

选项 1 允许您使用完全指定的位置来显式初始化成员变量。

回答by Salgar

There are many other reasons. You should always initialize all member variables in the initialization list if possible.

还有许多其他原因。如果可能,您应该始终初始化初始化列表中的所有成员变量。

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6

回答by Avada Kedavra

Option 1 allows you to initialize constmembers. This cannot be done with option 2 (as they are assigned to, not initialized).

选项 1 允许您初始化const成员。这不能用选项 2 完成(因为它们被分配给,未初始化)。

Why must const members be intialized in the constructor initializer rather than in its body?

为什么必须在构造函数初始值设定项中而不是在其主体中初始化 const 成员?