<legend id='QQsFK'><style id='QQsFK'><dir id='QQsFK'><q id='QQsFK'></q></dir></style></legend>

  • <i id='QQsFK'><tr id='QQsFK'><dt id='QQsFK'><q id='QQsFK'><span id='QQsFK'><b id='QQsFK'><form id='QQsFK'><ins id='QQsFK'></ins><ul id='QQsFK'></ul><sub id='QQsFK'></sub></form><legend id='QQsFK'></legend><bdo id='QQsFK'><pre id='QQsFK'><center id='QQsFK'></center></pre></bdo></b><th id='QQsFK'></th></span></q></dt></tr></i><div id='QQsFK'><tfoot id='QQsFK'></tfoot><dl id='QQsFK'><fieldset id='QQsFK'></fieldset></dl></div>

        • <bdo id='QQsFK'></bdo><ul id='QQsFK'></ul>

      1. <small id='QQsFK'></small><noframes id='QQsFK'>

      2. <tfoot id='QQsFK'></tfoot>

        为什么我们不能在声明时初始化类成员?

        时间:2023-09-26
        • <small id='F3Phn'></small><noframes id='F3Phn'>

            <tbody id='F3Phn'></tbody>
          <tfoot id='F3Phn'></tfoot>

          <legend id='F3Phn'><style id='F3Phn'><dir id='F3Phn'><q id='F3Phn'></q></dir></style></legend>
            <bdo id='F3Phn'></bdo><ul id='F3Phn'></ul>

                  <i id='F3Phn'><tr id='F3Phn'><dt id='F3Phn'><q id='F3Phn'><span id='F3Phn'><b id='F3Phn'><form id='F3Phn'><ins id='F3Phn'></ins><ul id='F3Phn'></ul><sub id='F3Phn'></sub></form><legend id='F3Phn'></legend><bdo id='F3Phn'><pre id='F3Phn'><center id='F3Phn'></center></pre></bdo></b><th id='F3Phn'></th></span></q></dt></tr></i><div id='F3Phn'><tfoot id='F3Phn'></tfoot><dl id='F3Phn'><fieldset id='F3Phn'></fieldset></dl></div>

                  本文介绍了为什么我们不能在声明时初始化类成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道为什么我们不能在成员声明时初始化成员.

                  I wonder if there is a reason why we can't initialize members at their declaration.

                  class Foo
                  {
                      int Bar = 42; // this is invalid
                  };
                  

                  相当于使用构造函数初始化列表.

                  As an equivalent of using constructor initialization lists.

                  class Foo
                  {
                      int Bar;
                  public:
                      Foo() : Bar(42) {}
                  }
                  

                  我个人的理解是,上面的例子更具表现力和意图.此外,这是一个较短的语法.而且我没有看到任何与其他语言元素混淆的可能性.

                  My personal understanding is that the above example is much more expressive and intentional. Moreover this is a shorter syntax. And I don't see any possibility of confusion with other language elements.

                  官方对此有澄清吗?

                  推荐答案

                  在 C++11 之前,不能像这样初始化非静态成员.如果您使用 C++11 编译器进行编译,它应该很乐意接受您提供的代码.

                  The initialization of non-static members could not be done like this prior to C++11. If you compile with a C++11 compiler, it should happily accept the code you have given.

                  我想首先不允许它的原因是因为数据成员声明不是定义.没有对象被引入.如果您有一个数据成员,例如 int x;,则在您实际创建类类型的对象之前,不会创建 int 对象.因此,此成员上的初始化程序会产生误导.只有在构造过程中才能给成员赋值,这正是成员初始化列表的作用.

                  I imagine that the reason for not allowing it in the first place is because a data member declaration is not a definition. There is no object being introduced. If you have a data member such as int x;, no int object is created until you actually create an object of the type of the class. Therefore, an initializer on this member would be misleading. It is only during construction that a value can be assigned to the member, which is precisely what member initialization lists are for.

                  在添加非静态成员初始化之前,还有一些技术问题需要解决.考虑以下示例:

                  There were also some technical issues to iron out before non-static member initialization could be added. Consider the following examples:

                  struct S {
                      int i(x);
                      // ...
                      static int x;
                  };
                  
                  struct T {
                      int i(x);
                      // ...
                      typedef int x;
                  };
                  

                  当解析这些结构体时,在解析成员i时,是数据成员声明(如S)还是一个不明确的成员函数声明(如T).

                  When these structs are being parsed, at the time of parsing the member i, it is ambiguous whether it is a data member declaration (as in S) or a member function declaration (as in T).

                  使用添加的功能,这不是问题,因为您无法使用此括号语法初始化成员.您必须使用 brace-or-equal-initializer 如:

                  With the added functionality, this is not a problem because you cannot initialize a member with this parantheses syntax. You must use a brace-or-equal-initializer such as:

                  int i = x;
                  int i{x};
                  

                  这些只能是数据成员,所以我们没有问题了.

                  These can only be data members and so we have no problem any more.

                  请参阅提案 N2628 以获得更全面的了解在提出非静态成员初始值设定项时必须考虑的问题.

                  See the proposal N2628 for a more thorough look at the issues that had to be considered when proposing non-static member initializers.

                  这篇关于为什么我们不能在声明时初始化类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在头文件与实现 (.cpp) 文件中定义构造函数 下一篇:为什么显式允许默认构造函数和具有 2 个或更多(非默认)参数的构造函数?

                  相关文章

                  <legend id='8TeqC'><style id='8TeqC'><dir id='8TeqC'><q id='8TeqC'></q></dir></style></legend>

                      <tfoot id='8TeqC'></tfoot>

                      • <bdo id='8TeqC'></bdo><ul id='8TeqC'></ul>

                      <small id='8TeqC'></small><noframes id='8TeqC'>

                      <i id='8TeqC'><tr id='8TeqC'><dt id='8TeqC'><q id='8TeqC'><span id='8TeqC'><b id='8TeqC'><form id='8TeqC'><ins id='8TeqC'></ins><ul id='8TeqC'></ul><sub id='8TeqC'></sub></form><legend id='8TeqC'></legend><bdo id='8TeqC'><pre id='8TeqC'><center id='8TeqC'></center></pre></bdo></b><th id='8TeqC'></th></span></q></dt></tr></i><div id='8TeqC'><tfoot id='8TeqC'></tfoot><dl id='8TeqC'><fieldset id='8TeqC'></fieldset></dl></div>