Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to force base class constructors to be called in derived classes?

Tags:

c++

basic c++ question i'm fairly sure. if i have a base class with a constructor that takes no parameters, and just initializes some of the protected members, does a derived class instantly call this base constructor too if it matches the parameters (wishful but unlikely thinking), and if not, is there a way to force it to automatically call said base constructor from the derived class WITHOUT having to explicitly tell it to do so in the derived class? I ask because i'm writing a wrapper of sorts and there are some protected members that i want initialized to specific values initially, and then i want to derive and manipulate this base class to my needs, but i wouldn't like an outside user to have to remember to explicitly call the base constructor or set these values within their own constructor.

like image 329
FatalCatharsis Avatar asked Apr 03 '12 21:04

FatalCatharsis


People also ask

How do you call a base class constructor from a derived class?

How to call the parameterized constructor of base class in derived class constructor? To call the parameterized constructor of base class when derived class's parameterized constructor is called, you have to explicitly specify the base class's parameterized constructor in derived class as shown in below program: C++

Does base class constructor get called automatically?

Using your example, the answer is: YES. The base constructor will be called for you and you do not need to add one. You are only REQUIRED to use "base(...)" calls in your derived class if you added a constructor with parameters to your Base Class, and didn't add an explicit default constructor.

Are base class constructors inherited by derived classes?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.

Can we load constructor in derived class?

Since the constructors can't be defined in derived class, it can't be overloaded too, in derived class.


2 Answers

Yes, the default base constructor is always called unless explicitly stated otherwise.

For example:

class A
{
public:
   A() { std::cout << "A"; }
};

class B : A
{
public:
   B() {}
};

int main()
{
   B b;
   return 0;
}

will output:

A

By "explicitly stated otherwise" I mean that you can call a different constructor from the derived class:

class A
{
public:
   A() { std::cout << "A"; }
   A(int) { std::cout << "AAA"; }
};

class B : A
{
public:
   B() : A(1) {}  //call A(int)
};

int main()
{
   B b;
   return 0;
}

will output

AAA

Important if you don't have a default constructor (you declare a non-default constructor and not a default one) or the default constructor is not visible (marked as private), you need to explicitly call an available constructor in the derived class.

like image 50
Luchian Grigore Avatar answered Oct 03 '22 20:10

Luchian Grigore


If your base-class has a "default constructor" (a constructor that takes no parameters; either explicitly provided by you, or implicitly provided by the compiler because you didn't explicitly provide any constructors), then every derived-class constructor will automatically call that unless you specify that they call a different constructor instead.

(If your base-class doesn't have a "default constructor", because you've provided one or more constructors that take parameters and no constructor that doesn't, then it's a compile-error for a derived-class constructor not to indicate the base-class constructor it calls.)

like image 25
ruakh Avatar answered Oct 03 '22 20:10

ruakh