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

Go To StackoverFlow.com

6

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.

2012-04-03 21:16
by FatalCatharsis
I am a little lost with the wording of your question, but if the base class has a default/no-argument constructor, it will be called when you initialize a derived class object if you don't explicitly specify the initialization of the base class - wkl 2012-04-03 21:20


8

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.

2012-04-03 21:20
by Luchian Grigore
let's say that the only constructor class A has, recieves an integer as a parameter, and then the derived class has a constructor that receives a single int as well. will the base constructor be called as well by chance? or is a constructor only considered a default constructor if it has no parameters - FatalCatharsis 2012-04-03 22:40
also, last thoughts, if it's vice versa, where you defined a constructor with parameters in B but a parameterless constructor in A, would the A's constructor be called? and also, which is called first, the base class' default constructor or the derived class' default constructor - FatalCatharsis 2012-04-03 22:59


2

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.)

2012-04-03 21:20
by ruakh
Ads