c++:在构造函数[duplicate]中初始化变量的位置

时间:2022-09-10 17:52:50

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如此常见?

6 个解决方案

#1


97  

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

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

  • 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.

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

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

    另外,正如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.

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

#2


19  

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

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

#3


8  

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.
  • 对于具有构造函数的类/结构体成员,使用选项1可能更有效。
  • only option 1 allows you to initialize reference members.
  • 只有选项1允许初始化引用成员。
  • only option 1 allows you to initialize const members
  • 只有选项1允许初始化const成员
  • only option 1 allows you to initialize base classes using their constructor
  • 只有选项1允许您使用它们的构造函数初始化基类
  • only option 2 allows you to initialize array or structs that do not have a constructor.
  • 只有选项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并不为人所知,它的优点也不为人所知。选项2的语法对新的c++程序员来说更自然。

#4


3  

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

选项1允许您使用指定的精确初始化成员变量的位置。

#5


2  

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 #常见问题- 10.6

#6


1  

Option 1 allows you to initialize const members. 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成员必须在构造函数初始化器中而不是在其主体中被初始化?

#1


97  

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

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

  • 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.

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

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

    另外,正如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.

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

#2


19  

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

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

#3


8  

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.
  • 对于具有构造函数的类/结构体成员,使用选项1可能更有效。
  • only option 1 allows you to initialize reference members.
  • 只有选项1允许初始化引用成员。
  • only option 1 allows you to initialize const members
  • 只有选项1允许初始化const成员
  • only option 1 allows you to initialize base classes using their constructor
  • 只有选项1允许您使用它们的构造函数初始化基类
  • only option 2 allows you to initialize array or structs that do not have a constructor.
  • 只有选项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并不为人所知,它的优点也不为人所知。选项2的语法对新的c++程序员来说更自然。

#4


3  

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

选项1允许您使用指定的精确初始化成员变量的位置。

#5


2  

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 #常见问题- 10.6

#6


1  

Option 1 allows you to initialize const members. 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成员必须在构造函数初始化器中而不是在其主体中被初始化?