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

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

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

        C++中的构造函数链

        Constructor chaining in C++(C++中的构造函数链)

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

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

                  <tbody id='bJL7y'></tbody>
                <tfoot id='bJL7y'></tfoot>
                • <bdo id='bJL7y'></bdo><ul id='bJL7y'></ul>
                  本文介绍了C++中的构造函数链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对构造函数链的理解是,当一个类中有多个构造函数(重载的构造函数)时,如果其中一个试图调用另一个构造函数,那么此过程称为 CONSTRUCTOR CHAINING ,C++ 不支持该过程.最近我在看网上资料的时候看到了这一段……是这样的……

                  My understanding of constructor chaining is that , when there are more than one constructors in a class (overloaded constructors) , if one of them tries to call another constructor,then this process is called CONSTRUCTOR CHAINING , which is not supported in C++ . Recently I came across this paragraph while reading online material.... It goes like this ...

                  您可能会发现自己想要编写一个成员函数来将类重新初始化为默认值.因为您可能已经有一个执行此操作的构造函数,所以您可能会尝试从您的成员函数调用构造函数.如前所述,链接构造函数调用在 C++ 中是非法的.您可以从函数中的构造函数复制代码,这会起作用,但会导致代码重复.在这种情况下,最好的解决方案是将代码从构造函数移到新函数中,并让构造函数调用您的函数来完成初始化数据的工作.

                  You may find yourself in the situation where you want to write a member function to re-initialize a class back to default values. Because you probably already have a constructor that does this, you may be tempted to try to call the constructor from your member function. As mentioned, chaining constructor calls are illegal in C++. You could copy the code from the constructor in your function, which would work, but lead to duplicate code. The best solution in this case is to move the code from the constructor to your new function, and have the constructor call your function to do the work of initializing the data.

                  调用构造函数的成员函数是否也属于构造函数链??请在 C++ 中阐明这个主题.

                  Does a member function calling the constructor also come under constructor chaining ?? Please throw some light on this topic in C++ .

                  推荐答案

                  该段基本上是这样说的:

                  The paragraph basically says this:

                  class X
                  {
                     void Init(params) {/*common initing code here*/ }
                     X(params1) { Init(someParams); /*custom code*/ } 
                     X(params2) { Init(someOtherParams); /*custom code*/ } 
                  };
                  

                  您也不能从成员函数调用构造函数.你可能觉得你已经做到了,但那是一种错觉:

                  You cannot call a constructor from a member function either. It may seem to you that you've done it, but that's an illusion:

                  class X
                  {
                  public:
                      X(int i):i(i){}
                      void f()
                      {
                         X(3); //this just creates a temprorary - doesn't call the ctor on this instance
                      }
                      int i;
                  };
                  
                  int main()
                  {
                      using std::cout;
                      X x(4);
                      cout << x.i << "
                  "; //prints 4
                      x.f();
                      cout << x.i << "
                  "; //prints 4 again
                  }
                  

                  这篇关于C++中的构造函数链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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(使用非平凡的构造函数初始化联合)
                  <legend id='JJxVB'><style id='JJxVB'><dir id='JJxVB'><q id='JJxVB'></q></dir></style></legend>
                • <tfoot id='JJxVB'></tfoot>

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

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

                            <bdo id='JJxVB'></bdo><ul id='JJxVB'></ul>
                              <tbody id='JJxVB'></tbody>