<tfoot id='MtC64'></tfoot>

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

      1. <legend id='MtC64'><style id='MtC64'><dir id='MtC64'><q id='MtC64'></q></dir></style></legend>
          <bdo id='MtC64'></bdo><ul id='MtC64'></ul>

        <i id='MtC64'><tr id='MtC64'><dt id='MtC64'><q id='MtC64'><span id='MtC64'><b id='MtC64'><form id='MtC64'><ins id='MtC64'></ins><ul id='MtC64'></ul><sub id='MtC64'></sub></form><legend id='MtC64'></legend><bdo id='MtC64'><pre id='MtC64'><center id='MtC64'></center></pre></bdo></b><th id='MtC64'></th></span></q></dt></tr></i><div id='MtC64'><tfoot id='MtC64'></tfoot><dl id='MtC64'><fieldset id='MtC64'></fieldset></dl></div>
      2. 使用 C++ 基类构造函数?

        Using C++ base class constructors?(使用 C++ 基类构造函数?)
          • <bdo id='Y5se7'></bdo><ul id='Y5se7'></ul>

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

                <tfoot id='Y5se7'></tfoot>
                  <tbody id='Y5se7'></tbody>

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

                  本文介绍了使用 C++ 基类构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在使用模板时,我遇到了需要使基类构造函数可从继承的类访问以创建对象以减少复制/粘贴操作.我想通过 using 关键字以与函数案例相同的方式来做到这一点,但那行不通.

                  While working with templates I ran into a need to make a base class constructors accessible from inherited classes for object creation to decrease copy/paste operations. I was thinking to do this through using keyword in same manner with functions case, but that not work.

                  class A
                  {
                  public: 
                      A(int val) {}
                  };
                  
                  class B : public A
                  {
                  };
                  
                  class C : public A
                  {
                  public:
                      C(const string &val) {}
                  };
                  
                  class D : public A
                  {
                  public:
                      D(const string &val) {}
                      using A::A;              // g++ error: A::A names constructor
                  };
                  
                  void main()
                  {
                      B b(10);                // Ok.   (A::A constructor is not overlapped)
                      C c(10);                // error: no matching function to call to 'C::C(int)'
                  }
                  

                  所以我的问题是:在继承类中的新构造函数被声明后,有没有办法导入基类构造函数?

                  So my question: Is there any way to import a base class constructors after new ones in inherited class been declared?

                  或者只有一种替代方法可以声明新的构造函数并从初始化列表中调用基本构造函数?

                  Or there is only one alternative to declare new constructors and call a base ones from initializer list?

                  推荐答案

                  首选初始化:

                  class C : public A
                  {
                  public:
                      C(const string &val) : A(anInt) {}
                  };
                  

                  在 C++11 中,您可以使用继承构造函数(其语法在您的示例 D 中可见).

                  In C++11, you can use inheriting constructors (which has the syntax seen in your example D).

                  更新:自 4.8 版以来,GCC 中已提供继承构造函数.

                  Update: Inheriting Constructors have been available in GCC since version 4.8.

                  如果您觉得初始化不吸引人(例如,由于实际情况中的可能性数量),那么对于某些 TMP 构造,您可能会喜欢这种方法:

                  If you don't find initialization appealing (e.g. due to the number of possibilities in your actual case), then you might favor this approach for some TMP constructs:

                  class A
                  {
                  public: 
                      A() {}
                      virtual ~A() {}
                      void init(int) { std::cout << "A
                  "; }
                  };
                  
                  class B : public A
                  {
                  public:
                      B() : A() {}
                      void init(int) { std::cout << "B
                  "; }
                  };
                  
                  class C : public A
                  {
                  public:
                      C() : A() {}
                      void init(int) { std::cout << "C
                  "; }
                  };
                  
                  class D : public A
                  {
                  public:
                      D() : A() {}
                      using A::init;
                      void init(const std::string& s) { std::cout << "D -> " << s << "
                  "; }
                  };
                  
                  int main()
                  {
                      B b; b.init(10);
                      C c; c.init(10);
                      D d; d.init(10); d.init("a");
                  
                      return 0;
                  }
                  

                  这篇关于使用 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(使用非平凡的构造函数初始化联合)
                  <i id='xUGjb'><tr id='xUGjb'><dt id='xUGjb'><q id='xUGjb'><span id='xUGjb'><b id='xUGjb'><form id='xUGjb'><ins id='xUGjb'></ins><ul id='xUGjb'></ul><sub id='xUGjb'></sub></form><legend id='xUGjb'></legend><bdo id='xUGjb'><pre id='xUGjb'><center id='xUGjb'></center></pre></bdo></b><th id='xUGjb'></th></span></q></dt></tr></i><div id='xUGjb'><tfoot id='xUGjb'></tfoot><dl id='xUGjb'><fieldset id='xUGjb'></fieldset></dl></div>
                      • <bdo id='xUGjb'></bdo><ul id='xUGjb'></ul>
                      • <tfoot id='xUGjb'></tfoot>
                            <tbody id='xUGjb'></tbody>
                          <legend id='xUGjb'><style id='xUGjb'><dir id='xUGjb'><q id='xUGjb'></q></dir></style></legend>

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