Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is constructor inheritance?

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?

like image 362
badmaash Avatar asked Apr 02 '12 15:04

badmaash


People also ask

Can we use constructor in inheritance?

No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors.

Why are constructors inherited?

Now suppose if constructors can be inherited then it will be impossible to achieving encapsulation. Because by using a super class's constructor we can access/initialize private members of a class. A constructor cannot be called as a method.

What happens to constructor in inheritance?

In the inheritance, the constructors never get inherited to any child class. In java, the default constructor of a parent class called automatically by the constructor of its child class.

How constructors are called in inheritance in Java?

Answer: Order of execution of constructors in inheritance relationship is from base /parent class to derived / child class. We know that when we create an object of a class then the constructors get called automatically.


1 Answers

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
like image 78
Andrew Tomazos Avatar answered Oct 10 '22 07:10

Andrew Tomazos