Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What happens if you don't call a base class's constructor for different derived constructors with different signatures?

Say I have a base and derived class like this:

class Base {
public:
    Base() { ... };
    Base(int param) { ... };
};

class Derived : public Base {
public:
    Derived() { ... };
    Derived(int param) { ... };
    Derived(int p1, int p2) { ... };
};

Note that I’m not explicitly calling any of Base‘s constructors for Derived‘s constructors. That is to say, I didn’t write Derived() : Base() { ... } or Derived(int param) : Base(param) { ... }.

Do any of Base‘s constructors get called by default when creating an instance of Derived?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If so, does Base(int param) get called when using Derived(int param), or does Base() get called?

Put another way, does C++ always default to using a base class’s constructor with the same signature as the derived class’s constructor if you don’t specify which constructor to use? Or does it just use the base class’s default constructor?

If the former, what about when using a constructor in the derived class that doesn’t have a matching constructor with the same signature in the base class, such as Derived(int p1, int p2)?

Please note this question does not relate to initialization of member variables of either class. I intentionally did not include any member variables in my pseudo-code. It specifically has to do with which constructor on the base class gets used if you do not explicitly specify a base constructor in the derived class’s constructors.

>Solution :

Quoting from cppreference’s description of constructors and how inheritance relates to them:

Before the compound statement that forms the function body of the constructor begins executing, initialization of all direct bases, virtual bases, and non-static data members is finished. Member initializer list is the place where non-default initialization of these objects can be specified. For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified. No initialization is performed for anonymous unions or variant members that do not have a member initializer.

(Emphasis added.)

If you do not specify an initialization of the base class subobject in your derived class’s constructor’s member initializer list, the base class subobject is default-initialized.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading