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

发布于 2024-07-09 09:46:57 字数 74 浏览 10 评论 0原文

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

两者有什么区别?

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

What is the difference between the two?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

请叫√我孤独 2024-07-16 09:46:57

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

你说:

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

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

1) const 对象只能被初始化

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

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

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

You said:

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

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

1) A const object can only be initialised.

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

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.

月亮坠入山谷 2024-07-16 09:46:57

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

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

将产生相当于的代码;

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

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

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

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

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

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

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

更多关于 cpp-tutorial代码牧马人

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

 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.

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.

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-tutorial and Code Wrangler.

表情可笑 2024-07-16 09:46:57

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

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

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