c++11的构造函数继承

https://en.cppreference.com/w/cpp/language/using_declaration

在[Inheriting constructors]这一节.

其实叫做"基类的构造函数前置"比较好.

像mystring继承自string类,但仍然是提供字符串功能.new的时候仍旧希望保留旧有的初始化传参方式.这时候在mystring里一一再实现(调用基类)就比较麻烦.

但在c++11之前只能这样.

c++11之前的默认构造方式(淡然c++11之后还是)是,先把各个基类的默认初始化函数调用一遍.然后对派生字段做默认初始化.

c++11之前用户定义的构造函数,可以在初始化列表里调用基类的构造函数,同时可以传参.基类的构造函数仍旧是先调用一遍.

c++11开始,就有了using Base::Base;的写法,当编译器在派生类里找不到相应的构造函数时,就会在这个基类Base里找.在此之后,会调用其它基类的默认构造函数,派生类的字段则是赋值初始化.(这也是c++11之后引入的)

Inheriting constructors

If the using-declaration refers to a constructor of a direct base of the class being defined (e.g. using Base::Base;), all constructors of that base (ignoring member access) are made visible to overload resolution when initializing the derived class.

If overload resolution selects an inherited constructor, it is accessible if it would be accessible when used to construct an object of the corresponding base class: the accessibility of the using-declaration that introduced it is ignored.

If overload resolution selects one of the inherited constructors when initializing an object of such derived class, then the Base subobject from which the constructor was inherited is initialized using the inherited constructor, and all other bases and members of Derived are initialized as if by the defaulted default constructor (default member initializers are used if provided, otherwise default initialization takes place). The entire initialization is treated as a single function call: initialization of the parameters of the inherited constructor is sequenced-before initialization of any base or member of the derived object.

翻译下来大概是:

using Base::Base;的用途:

>派生类初始化时,基类的所有构造函数对overload resolution可见,无视权限修饰符.

>如果基类的某个构造函数被选中,那么先用它初始化那个基类,然后"其它的基类"和"派生字段"被初始化就像使用默认的default 构造函数.(采用提供的default initializers,若用户未提供,则发生默认初始化).

>那个基类的构造函数总是最先被调用.

下面是例子代码:

struct B1 {  B1(int, ...) { } };
struct B2 {  B2(double)   { } };
 
int get();
 
struct D1 : B1 {
  using B1::B1;  // inherits B1(int, ...)
  int x;
  int y = get();
};
 
void test() {
  D1 d(2, 3, 4); // OK: B1 is initialized by calling B1(2, 3, 4),
                 // then d.x is default-initialized (no initialization is performed),
                 // then d.y is initialized by calling get()
  D1 e;          // Error: D1 has no default constructor
}
 
struct D2 : B2 {
  using B2::B2; // inherits B2(double)
  B1 b;
};
 
D2 f(1.0);       // error: B1 has no default constructor

As with using-declarations for any other non-static member functions, if an inherited constructor matches the signature of one of the constructors of Derived, it is hidden from lookup by the version found in Derived. If one of the inherited constructors of Base happens to have the signature that matches a copy/move constructor of the Derived, it does not prevent implicit generation of Derived copy/move constructor (which then hides the inherited version, similar to using operator=).

翻译:简单的说,就是using Base:Base之后,派生类可以重载某一个版本来覆盖掉. 基类的copy/move constructor不受这个using的影响,派生类如果没有copy/move constructor,编译器会生成默认的,不会用基类的.

例子代码:

struct B1 {   B1(int); };
struct B2 {   B2(int); };
 
struct D2 : B1, B2 {
  using B1::B1;
  using B2::B2;
  D2(int);   // OK: D2::D2(int) hides both B1::B1(int) and B2::B2(int)
};
D2 d2(0);    // calls D2::D2(int)

相关推荐