What is constructor inheritance?

27,252

Inheriting Constructors means just that. A derived class can implicitly inherit constructors from its base class(es).

The syntax is as follows:

struct B
{
    B(int); // normal constructor 1
    B(string); // normal constructor 2
};

struct D : B
{
    using B::B; // inherit constructors from B
};

So now D has the following constructors implicitly defined:

D::D(int); // inherited
D::D(string); // inherited

Ds members are default constructed by these inherited constructors.

It is as though the constructors were defined as follows:

D::D(int x) : B(x) {}
D::D(string s) : B(s) {}

The feature isn't anything special. It is just a shorthand to save typing boilerplate code.

Here are the gory details:

12.9 Inheriting Constructors

1) A using-declaration that names a constructor implicitly declares a set of inheriting constructors. The candidate set of inherited constructors from the class X named in the using-declaration consists of actual constructors and notional constructors that result from the transformation of defaulted parameters as follows:

  • all non-template constructors of X, and
  • for each non-template constructor of X that has at least one parameter with a default argument, the set of constructors that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list, and
  • all constructor templates of X, and
  • for each constructor template of X that has at least one parameter with a default argument, the set of constructor templates that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list
Share:
27,252
badmaash
Author by

badmaash

Updated on April 03, 2020

Comments

  • badmaash
    badmaash about 4 years

    In C++11, what is meant by inheriting the constructor? If it is what i think it is (Base class constructor is brought in the scope of the derived class), what are its implications on my code? What are the applications of such a feature?

  • Alok Save
    Alok Save about 12 years
    I don't think @badmaash asked for a copy paste from the Standard.Badmaash, Did you check the wiki link posted under the Q?
  • badmaash
    badmaash about 12 years
    @Als, i just read that, but i still do not understand how inheriting the constructors are going to initialize the derived class data members.
  • Andrew Tomazos
    Andrew Tomazos about 12 years
    @badmaash: See my example. Ds members are default constructed.
  • badmaash
    badmaash about 12 years
    @user1131467, what if i provide my own constructor apart from the inherited one? What if i write D(int){} in D? What happens then?
  • Andrew Tomazos
    Andrew Tomazos about 12 years
    @badmaash: user declaration supersedes implicit declaration. The explicitly defined D(int) takes precedence, and B(int) is not inherited.
  • Aaron McDaid
    Aaron McDaid over 10 years
    Question: are 'copy constructors' and 'move constructors' inherited? My experiments (clang3.3) suggest not, but I can't find text to confirm this.
  • Andrew Tomazos
    Andrew Tomazos over 10 years
    @AaronMcDaid: Default constructors and copy/move constructors are not inherited no. See 12.9 [class.inhctor] p3. They are part of the candidate set, but excluded from the actual set of constructors inherited. See the example in p6 for clarification. They are usually implicitly defined so inheritance is unnecessary.
  • Kerrek SB
    Kerrek SB about 8 years
    Can you please extend the examples to include default arguments?