<tfoot id='KMTAf'></tfoot>

<small id='KMTAf'></small><noframes id='KMTAf'>

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

  • <legend id='KMTAf'><style id='KMTAf'><dir id='KMTAf'><q id='KMTAf'></q></dir></style></legend>

        • <bdo id='KMTAf'></bdo><ul id='KMTAf'></ul>
      1. 声明时新的 C++11 成员初始化功能是否使初始化列表过时了?

        Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)

        <small id='RlyFm'></small><noframes id='RlyFm'>

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

                  <tbody id='RlyFm'></tbody>
                <tfoot id='RlyFm'></tfoot>
                1. 本文介绍了声明时新的 C++11 成员初始化功能是否使初始化列表过时了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 C++11,我们现在可以在头声明中初始化类成员:

                  class aClass{私人的:int mint{100};上市:一类();~aClass();};

                  所以我有点困惑.传统上,构造函数中的初始化列表已用于成员初始化:

                  aClass::aClass():分钟(100){...}

                  声明时新的 C++11 成员初始化功能是否使初始化列表过时了?如果不是,一个比另一个有什么优势?什么情况会使声明时初始化或初始化列表有利?什么时候应该使用一个?

                  解决方案

                  不,它们并没有像本文一样过时 了解新的 C++11 初始化形式在类成员初始化部分(强调我的)说:><块引用>

                  请记住,如果同一个数据成员在构造函数中同时具有类成员初始值设定项和 mem-init,则后者优先.事实上,您可以通过以类成员初始值设定项的形式为成员指定默认值来利用此行为,如果构造函数没有显式的 mem-init for那个成员.否则,构造函数的 mem-init 将生效,覆盖类成员初始值设定项.此技术在具有多个构造函数的类中很有用

                  因此,尽管在类成员中初始化是一个很好的便利,但它并没有消除对初始化列表的需要,而是两个功能一起工作,为您提供了一种指定默认值并在需要时覆盖它们的好方法.他说,这似乎也是 Bjarne Stroustrup 的看法:

                  <块引用>

                  这节省了一些输入,但真正的好处来自具有多个构造函数的类.通常,所有构造函数都为一个成员使用一个通用的初始化器:

                  并提供具有公共初始化程序的成员示例:

                  class A {上市:A(): a(7), b(5), hash_algorithm("MD5"), s("Constructor run") {}A(int a_val) : a(a_val), b(5), hash_algorithm("MD5"), s("Constructor run") {}A(D d) : a(7), b(g(d)), hash_algorithm("MD5"), s("Constructor run") {}国际a, b;私人的:HashingFunction hash_algorithm;//应用于所有 A 实例的加密哈希std::string s;//指示对象生命周期状态的字符串};

                  并说:

                  <块引用>

                  hash_algorithm 和 s 每个都有一个默认值这一事实在代码混乱中丢失了,并且很容易在维护期间成为问题.相反,我们可以分解数据成员的初始化:

                  class A {上市:A(): a(7), b(5) {}A(int a_val) : a(a_val), b(5) {}A(D d) : a(7), b(g(d)) {}国际a, b;私人的:HashingFunction hash_algorithm{"MD5"};//应用于所有 A 实例的加密哈希std::string s{"构造函数运行"};//指示对象生命周期状态的字符串};

                  注意:C++11 的缺点

                  在 C++11 中使用类成员初始化有一个缺点,因为它使类成为非聚合我们不能再使用 聚合初始化 这可能相当令人惊讶.在删除此限制的 C++14 中,情况并非如此.请参阅:具有非静态成员初始值设定项的类的 C++11 聚合初始化,了解更多详细信息.

                  With C++11, we now have the ability to initialize class members in a header declaration:

                  class aClass
                  {
                      private:
                          int mInt{100};
                      public:
                           aClass();
                          ~aClass();
                  };
                  

                  So I'm a bit confused. Traditionally initialization lists in constructors have been used for member initialization:

                  aClass::aClass()
                  : mInt(100)
                  {
                      ...
                  }
                  

                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete? If not, what are the advantages of one over the other? What situations would make initialization at declaration advantageous, or initialization lists advantageous? When should one be used over the other?

                  解决方案

                  No, they are not obsolete as this article Get to Know the New C++11 Initialization Forms says in the Class Member Initialization section (emphasis mine):

                  Bear in mind that if the same data member has both a class member initializer and a mem-init in the constructor, the latter takes precedence. In fact, you can take advantage of this behavior by specifying a default value for a member in the form of a class member initializer that will be used if the constructor doesn't have an explicit mem-init for that member. Otherwise, the constructor's mem-init will take effect, overriding the class member initializer. This technique is useful in classes that have multiple constructors

                  So although in class member initialization is a nice convenience it does not remove the need for initialization lists but both features instead work together to give you a nice way to specify default values and override them when needed. This seems to be also how Bjarne Stroustrup sees it too, he says:

                  This saves a bit of typing, but the real benefits come in classes with multiple constructors. Often, all constructors use a common initializer for a member:

                  and provides an example of members which have a common initializer:

                  class A {
                    public:
                      A(): a(7), b(5), hash_algorithm("MD5"), s("Constructor run") {}
                      A(int a_val) : a(a_val), b(5), hash_algorithm("MD5"), s("Constructor run") {}
                      A(D d) : a(7), b(g(d)), hash_algorithm("MD5"), s("Constructor run") {}
                      int a, b;
                    private:
                      HashingFunction hash_algorithm;  // Cryptographic hash to be applied to all A instances
                      std::string s;                   // String indicating state in object lifecycle
                  };
                  

                  and says:

                  The fact that hash_algorithm and s each has a single default is lost in the mess of code and could easily become a problem during maintenance. Instead, we can factor out the initialization of the data members:

                  class A {
                    public:
                      A(): a(7), b(5) {}
                      A(int a_val) : a(a_val), b(5) {}
                      A(D d) : a(7), b(g(d)) {}
                      int a, b;
                    private:
                      HashingFunction hash_algorithm{"MD5"};  // Cryptographic hash to be applied to all A instances
                      std::string s{"Constructor run"};       // String indicating state in object lifecycle
                  };
                  

                  Note: disadvantage in C++11

                  There is one disadvantage to using in class member initialization in C++11 since it makes a class a non-aggregate we can no longer use aggregate initialization which may be rather surprising. This is not the case in C++14 where this restriction was removed. See: C++11 aggregate initialization for classes with non-static member initializers for more details.

                  这篇关于声明时新的 C++11 成员初始化功能是否使初始化列表过时了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                  Why is the copy-constructor argument const?(为什么复制构造函数参数是常量?)
                2. <small id='otyFJ'></small><noframes id='otyFJ'>

                      • <bdo id='otyFJ'></bdo><ul id='otyFJ'></ul>
                          <tbody id='otyFJ'></tbody>

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