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?
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