Constructors of inherited c ++ 11

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

In [ Inheriting Constructors ] this section.

In fact, is called "the base class constructor front" is better.

Like mystring inherited from the string class, but still provides a string function. new time still want to retain the old initialization parameter passing mode. In this time and again to achieve a mystring in (call the base class) is more trouble.

But before this can only c ++ 11.

The default configuration mode before c ++ 11 (after 11 or indifferent c ++), the first default initialization function call over each of the base class. Then the field is derived do default initialization.

c ++ user-defined constructor, you can call before 11 in the initialization list the base class constructor, and can pass parameters. The base class constructor is still the first call again.

c ++ 11 began, there was using Base :: Base; wording, when the compiler can not find the appropriate constructor in a derived class, you will find in this base class Base in. After that, it will call the default constructor other base class, the derived class fields are initialized assignment. (This is also introduced after 11 c ++)

 

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.

 

Translation is probably down:

using Base :: Base; use:

> Derived class initialization, all of the base class constructor overload resolution visible, ignoring permission modifier.

> If a base class constructor is selected, then use it to initialize the base class, then "Other base class" and "derived field" is initialized as the default default constructor. (Using default initializers provided, if the user does not provide a default initialization occurs).

> Constructor of the base class are always the first to be called.

Here is an example of code:

struct B1 {  B1(int, ...) { } };
struct B2 {  B2(double)   { } };
 
you  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=).

Translation: Simply put, that is, using Base: After Base, derived classes can override a version to overwrite. copy of the base class / move constructor without using this effect, the derived class if there is no copy / move constructor, the compiler generates a default, it will not use the base class.

Example code:

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)

 

 

Guess you like

Origin www.cnblogs.com/xiang-yin/p/12075732.html