<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++ 基类构造函数?

        时间:2023-09-26
          • <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++ 基类构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std:ostringstream 和 -std=c++11 的内存错误? 下一篇:在头文件与实现 (.cpp) 文件中定义构造函数

                  相关文章

                  <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>
                  1. <tfoot id='xUGjb'></tfoot>
                      <legend id='xUGjb'><style id='xUGjb'><dir id='xUGjb'><q id='xUGjb'></q></dir></style></legend>

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