71

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?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
badmaash
  • 4,775
  • 7
  • 46
  • 61
  • 2
    This wiki: [Object construction Improvement](http://en.wikipedia.org/wiki/C%2B%2B11#Object_construction_improvement) answers your question aptly in simple lucid language. – Alok Save Apr 02 '12 at 15:33

1 Answers1

91

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
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • I don't think @badmaash asked for a copy paste from the Standard.Badmaash, Did you check the wiki link posted under the Q? – Alok Save Apr 02 '12 at 15:36
  • 2
    @Als, i just read that, but i still do not understand how inheriting the constructors are going to initialize the derived class data members. – badmaash Apr 02 '12 at 15:49
  • 1
    @badmaash: See my example. Ds members are default constructed. – Andrew Tomazos Apr 02 '12 at 15:50
  • @user1131467, what if i provide my own constructor apart from the inherited one? What if i write `D(int){}` in D? What happens then? – badmaash Apr 02 '12 at 16:04
  • 1
    @badmaash: user declaration supersedes implicit declaration. The explicitly defined D(int) takes precedence, and B(int) is not inherited. – Andrew Tomazos Apr 02 '12 at 16:05
  • 2
    Question: are 'copy constructors' and 'move constructors' inherited? My experiments (clang3.3) suggest not, but I can't find text to confirm this. – Aaron McDaid Nov 03 '13 at 10:41
  • 1
    @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. – Andrew Tomazos Nov 03 '13 at 10:53
  • 2
    Can you please extend the examples to include default arguments? – Kerrek SB May 06 '16 at 09:32
  • Why do you say "implicitly" derive constructors, if you need to spell that out with the `using` directive? I think you are borrowing the word from the text you are citing, but in an ambiguous way. – Antonio Nov 10 '22 at 11:37
  • @Antonio: I mean they are "implicitly" defined for the user as opposed to "explicitly" defined by the user - as in the constructors are generated for the user by the compiler (because of the using directive). I do not mean to refer to any formal use of the term "implicit" in the standard. – Andrew Tomazos Nov 10 '22 at 12:11