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

      <tfoot id='JuwZP'></tfoot>

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

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

        在构造函数中初始化字段 - 初始化列表与构造函数主体

        Initializing fields in constructor - initializer list vs constructor body(在构造函数中初始化字段 - 初始化列表与构造函数主体)
          <bdo id='AqgEj'></bdo><ul id='AqgEj'></ul>

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

                <tbody id='AqgEj'></tbody>

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

                  本文介绍了在构造函数中初始化字段 - 初始化列表与构造函数主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用 C++ 已经有一段时间了,但我不确定

                  I have been working in c++ for some time now, but I am unsure about the difference between

                  public : Thing(int _foo, int _bar): member1(_foo), member2(_bar){}
                  

                  public : Thing(int _foo, int _bar){
                      member1 = _foo;
                      member2 = _bar;
                  }
                  

                  我感觉它们做同样的事情,但这两种语法之间是否存在实际差异.其中一个比另一个更安全吗,它们是否以不同的方式处理默认参数.

                  I have a feeling that they do the same thing, but is there a practical difference between these 2 syntaxes. Is one of these safer then the other, and do they handle default parameters differently.

                  对第一个例子不太习惯,所以如果我犯了错误,我道歉.

                  Not totally accustomed to the first example so if I made a mistake on it I apologize.

                  推荐答案

                  如果 member1member2 是非 POD(即非Plain Old Data) 类型:

                  They are not the same if member1 and member2 are non-POD (i.e. non-Plain Old Data) types:

                  public : Thing(int _foo, int _bar){
                      member1 = _foo;
                      member2 = _bar;
                  }
                  

                  相当于

                  public : Thing(int _foo, int _bar) : member1(), member2(){
                      member1 = _foo;
                      member2 = _bar;
                  }
                  

                  因为它们会在构造函数体开始执行之前被初始化,所以基本上完成了两倍的工作.这也意味着,如果这些成员的类型没有默认构造函数,那么您的代码将不会编译.

                  because they will be initialized before the constructor body starts executing, so basically twice the work is done. That also means, if the type of these members don't have default constructor, then your code will not compile.

                  这篇关于在构造函数中初始化字段 - 初始化列表与构造函数主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

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

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

                        <bdo id='ZqrLn'></bdo><ul id='ZqrLn'></ul>

                          <tbody id='ZqrLn'></tbody>
                      • <legend id='ZqrLn'><style id='ZqrLn'><dir id='ZqrLn'><q id='ZqrLn'></q></dir></style></legend><tfoot id='ZqrLn'></tfoot>