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

        <i id='dDBKB'><tr id='dDBKB'><dt id='dDBKB'><q id='dDBKB'><span id='dDBKB'><b id='dDBKB'><form id='dDBKB'><ins id='dDBKB'></ins><ul id='dDBKB'></ul><sub id='dDBKB'></sub></form><legend id='dDBKB'></legend><bdo id='dDBKB'><pre id='dDBKB'><center id='dDBKB'></center></pre></bdo></b><th id='dDBKB'></th></span></q></dt></tr></i><div id='dDBKB'><tfoot id='dDBKB'></tfoot><dl id='dDBKB'><fieldset id='dDBKB'></fieldset></dl></div>
          <bdo id='dDBKB'></bdo><ul id='dDBKB'></ul>
      1. <tfoot id='dDBKB'></tfoot>
        <legend id='dDBKB'><style id='dDBKB'><dir id='dDBKB'><q id='dDBKB'></q></dir></style></legend>
      2. 虚继承中构造函数调用的顺序

        Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
        <tfoot id='hPDCq'></tfoot>
          <tbody id='hPDCq'></tbody>
            • <legend id='hPDCq'><style id='hPDCq'><dir id='hPDCq'><q id='hPDCq'></q></dir></style></legend>

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

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

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

                  本文介绍了虚继承中构造函数调用的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  class A {
                          int i;
                  public: 
                          A() {cout<<"in A's def const
                  ";};
                          A(int k) {cout<<"In A const
                  ";  i = k; }
                          };
                  
                  class B : virtual public A {
                  public:
                          B(){cout<<"in B's def const
                  ";};
                          B(int i) : A(i) {cout<<"in B const
                  ";}
                          };
                  
                  class C :   public B {
                  public:
                          C() {cout<<"in C def cstr
                  ";}
                          C(int i) : B(i) {cout<<"in C const
                  ";}
                          };
                  
                  int main()
                  {
                          C c(2);
                          return 0;
                  }
                  

                  这种情况下的输出是

                  in A's def const
                  in B const
                  in C const
                  

                  为什么这不进入in A const

                  `它应该遵循 1 arg 构造函数调用的顺序.但是使用 virtual 关键字从 A 派生 B 时实际发生了什么.

                  `It should follow the order of 1 arg constructor call. But what actually is happening on deriving B from A using virtual keyword.

                  还有几个问题

                  即使我删除了上面程序中的 virtual 关键字并删除了所有默认构造函数,它也会出错.那么,为什么它需要 def 构造函数

                  Even if I remove the virtual keyword in above program and remove all the default constructor it gives error. So, why it needs the def constructor

                  推荐答案

                  虚基类的构造函数总是从最派生的类调用,使用它可能传入的任何参数.在你的情况下,最派生的类不会't 为 A 指定初始化器,因此使用默认构造函数.

                  The constructors for virtual base classes are always called from the most derived class, using any arguments it might pass in. In your case, the most derived class doesn't specify an initializer for A, so the default constructor is used.

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

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

                  相关文档推荐

                  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 成员初始化功能是否使初始化列表过时了?)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                  Why is the copy-constructor argument const?(为什么复制构造函数参数是常量?)
                    <bdo id='3vyUD'></bdo><ul id='3vyUD'></ul>

                      <small id='3vyUD'></small><noframes id='3vyUD'>

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

                        1. <tfoot id='3vyUD'></tfoot>