1

I am new to c++ I have 2 queries regarding how a base constructor is called.

1.) Lets say my code looks somewhat like this.

#include<iostream>
using namespace std;

class Base  {
public:
    Base()    { cout<<"Constructor: Base"<<endl; }
    virtual ~Base()   { cout<<"Destructor : Base"<<endl; }
};

class Derived: public Base {
public:
    Derived()   { cout<<"Constructor: Derived"<<endl; }
    ~Derived()  { cout<<"Destructor : Derived"<<endl; }
};

int main()  {
    Base *Var = new Derived();
    delete Var;
    return 0;
}

I was told that a base constructor has to be called explicitly before defining the derived constructor by initialization list. But here without any call to the base constructor, the code is working as expected.

**The output for the above problem is**

Constructor: Base
Constructor: Derived
Destructor : Derived
Destructor : Base

2.) Now suppose the base class constructor takes parametrized arguments, but the derived constructor is empty. In the main function, I declare a derived object without any argument to the constructor. what will happen? Any way to separately pass the base constructor of that object an argument?

Thanks.

MultiVAC
  • 354
  • 1
  • 10
starkk92
  • 5,754
  • 9
  • 43
  • 59

3 Answers3

1

If the Base constructor you want to call from Derived isn't the default, simply use an initialisation list, something like:

Derived::Derived() : Base(args) {
    // ...
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Isnt that called "constructor forwarding"? For my understanding an initialisation list or "initializer list" wold be the "curly braces value list" for initializing instances – Ole Dittmann Jun 05 '14 at 11:38
  • @OleDittmann specifically it's called a `member initializer list` and it's not to be confused with `list initialization` which is what your thinking of. – Paul Evans Jun 05 '14 at 11:47
0

You do not need to call the base constructor in a derived one. This is done automatically - always. If you want to call a constructor explicitly you may do so at any time by specifying the class namespace and constructor (which should be the same) like this:

PointerToObject->ClassName::ClassName(Parameters);

(But this is considederd a "NoNo!" - bad style, because it should not be neccessary in case of good design)

Ole Dittmann
  • 1,764
  • 1
  • 14
  • 22
  • Ummm... really? ;-P See ideone.com example of your code [here](http://ideone.com/kKAZBT), or did you mean something different...? – Tony Delroy Jun 05 '14 at 11:17
  • Hmm, ok, VisualC++ has no problem with that. Anyway, using constructor forwarding as already proposed would be the correct way here. – Ole Dittmann Jun 05 '14 at 11:30
0

first of all constructor of base class is called. it is so because it is inherited in derived class so the order to call the constructors would be like that the class which is inherited first ,its constructor is called first and then the constructor of the class which is inheriting the base class is called. let us take an example

class a: public b,public c,public d
 { ....
  }

then it will call the constructor of class b first then c then d and then a.

In the case of destructor, you have made the destructor of base class as virtual so when you writing delete Var , it is calling destructor of derived class. and at the end to delete all the objects, the destructor of base class is called.

vivek puri
  • 59
  • 7