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

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

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

c++constructorinitializationconst

提问by mahesh

Why must class members declared as constbe initialized in the constructor initializer list rather than in the constructor body?

为什么必须const在构造函数初始值设定项列表中而不是在构造函数体中初始化声明为的类成员?

What is the difference between the two?

两者有什么区别?

回答by Vladimir

In C++, an object is considered fully initialised when execution enters the body of the constructor.

在 C++ 中,当执行进入构造函数体时,对象被视为完全初始化。

You said:

你说:

"i wanted to know why const must be intialized in constructor initializer list rather than in it's body ?."

“我想知道为什么必须在构造函数初始值设定项列表中而不是在它的主体中初始化 const ?”

What you are missing is that initialisationhappens in the initialisation list, and assignmenthappens in the body of the constructor. The steps in logic:

您缺少的是初始化发生在初始化列表中,而赋值发生在构造函数的主体中。逻辑步骤:

1) A const object can only be initialised.

1) const 对象只能被初始化。

2) An object has all of its members initialised in the initialisation list. Even if you do not explicitly initialise them there, the compiler will happily do so for you :-)

2) 一个对象的所有成员都在初始化列表中进行了初始化。即使您没有在那里显式初始化它们,编译器也会很乐意为您这样做:-)

3) Therefore, putting 1) and 2) together, a member which is const can only ever have a value assigned to it at initialisation, which happens during the initialisation list.

3) 因此,将 1) 和 2) 放在一起,一个 const 成员只能在初始化时分配给它一个值,这发生在初始化列表期间。

回答by VonC

constand reference variables must be initialized on the line they are declared.

const和引用变量必须在它们声明的行上初始化。

 class Something  
 {  
     private:  
      const int m_nValue;  
     public:  
      Something()  
      {  
          m_nValue = 5;  
      }  
  };

would produce code equivalent to;

将产生等效于的代码;

const int nValue; // error, const vars must be assigned values immediately  
nValue = 5; 

Assigning const or reference member variables values in the body of the constructor is not sufficient.

在构造函数的主体中分配 const 或引用成员变量值是不够的。

C++ provides another way of initializing member variables that allows to initialize member variables when they are created rather than afterwards. This is done through use of an initialization list.

C++ 提供了另一种初始化成员变量的方法,允许在创建成员变量时而不是之后初始化它们。这是通过使用初始化列表来完成的。

You can assign values to variables in two ways: explicitly and implicitly: view plaincopy to clipboardprint?

您可以通过两种方式为变量赋值:显式和隐式:查看普通副本到剪贴板打印?

int nValue = 5; // explicit assignment  
double dValue(4.7); // implicit assignment  

Using an initialization list is very similar to doing implicit assignments.

使用初始化列表与进行隐式赋值非常相似。

Remember that the member initialization list, used to initialize base and member data objects, is in the definition, not declaration of the constructor.

请记住,用于初始化基类和成员数据对象的成员初始化列表在定义中,而不是在构造函数的声明中。

More on cpp-tutorialand Code Wrangler.

更多关于cpp-tutorialCode Wrangler 的信息

回答by user3651946

Because constant variables and references must be initialized at time of declaration i.e before use. But Constructors will assign value to a varaible not initailize the variable therefore you must use initailizier list for constant and references

因为常量变量和引用必须在声明时即使用前初始化。但是构造函数会将值分配给变量而不是初始化变量,因此您必须对常量和引用使用初始化列表