• <tfoot id='kgzz9'></tfoot>

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

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

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

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

        创建派生类对象时将参数传递给基类构造函数

        Pass Parameter to Base Class Constructor while creating Derived class Object(创建派生类对象时将参数传递给基类构造函数)
          <tfoot id='THsoQ'></tfoot>

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

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

                  本文介绍了创建派生类对象时将参数传递给基类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑两个类 AB

                  class A
                  {
                  public:
                      A(int);
                      ~A();
                  };
                  
                  class B : public A
                  {
                  public:
                      B(int);
                      ~B();
                  };
                  
                  
                  int main()
                  {
                      A* aobj;
                      B* bobj = new bobj(5);    
                  }
                  

                  现在B类继承了A.

                  我想创建一个B的对象.我知道创建派生类对象也会调用基类构造函数,但这是没有任何参数的默认构造函数.

                  I want to create an object of B. I am aware that creating a derived class object, will also invoke the base class constructor , but that is the default constructor without any parameters.

                  我想要的是 B 接受一个参数(比如 5),并将它传递给 A 的构造函数.请展示一些代码来演示这个概念.

                  What i want is that B to take a parameter (say 5), and pass it on to the constructor of A. Please show some code to demonstrate this concept.

                  推荐答案

                  使用基成员初始化:

                  class B : public A
                  {
                  public:
                      B(int a) : A(a)
                      {
                      }
                      ~B();
                  };
                  

                  这篇关于创建派生类对象时将参数传递给基类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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(使用非平凡的构造函数初始化联合)
                2. <legend id='6fKOt'><style id='6fKOt'><dir id='6fKOt'><q id='6fKOt'></q></dir></style></legend>
                    <tbody id='6fKOt'></tbody>
                      <tfoot id='6fKOt'></tfoot>
                      1. <small id='6fKOt'></small><noframes id='6fKOt'>

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