iFreilicht
iFreilicht

Reputation: 14474

Is the deletion of a constructor inherited?

Using the keyword delete you can prevent the compiler from automatically adding standard implementations of certain constructors.

Is this deletion inherited to subclasses?

Upvotes: 8

Views: 2338

Answers (2)

Brian Bi
Brian Bi

Reputation: 119099

Using the keyword delete you can prevent the compiler from automatically adding standard implementations of certain constructors.

Well, not really. Yes, it does accomplish that purpose. But it also prevents you from implementing that constructor yourself.

Constructors are not inherited, in general. Deleting a constructor in the base class does not cause the corresponding constructor in the derived class to be deleted too. It does force the compiler to implicitly define the corresponding constructor in the derived class as deleted---but only if you don't define it yourself.

Upvotes: 0

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

Apparently, but I wouldn't exactly say that the attribute is inheirited. It is due to the fact that the compiler generated derived class constructor uses the base class constructor. For example, the compiler generated default constructor of a derived class uses the default constructor of the base class. So if the base class default constructor does not exist, for whatever reason (whether it was explicitly deleted, or some other reason), the compiler cannot generate a default constructor for the derived class. But this doesn't stop you from creating your own constructor for the derived class which uses a different base class constructor than the one which was deleted.

Upvotes: 10

Related Questions