Constructor Initialiser List

Base class constructor will be called before entering into the derived class constructor definition. 

 #include <iostream>
using namespace std;

class Army  
{
private:
    int armyCode;
public:
    Army() { cout << " Base: Default Ctor " << __func__ << endl; };
    Army(int armyCode) { cout << " Base: Parameterised Ctor " << __func__ << endl; };
};

class ArmyHospital : public Army {
public:
    ArmyHospital() {
       // Before entering this line,
       // base class constructor is called
       cout << " Derived: " << __func__ << endl;
    }
};

int main ()
{
    ArmyHospital arm1;
    return 1;
}


1
2
 Base: Default Ctor Army
 Derived: ArmyHospital


Why can't we invoke the base constructor inside the derived class constructor ? Let's see.

 #include <iostream>
using namespace std;

class Army  
{
private:
    int armyCode;
public:
    Army() { cout << " Base: Default Ctor " << __func__ << endl; };
    Army(int armyCode) { cout << " Base: Parameterised Ctor " << __func__ << endl; };
};

class ArmyHospital : public Army {\
private:
    int code_;
public:
    ArmyHospital(int cod){
       cout << " Start Derived: " << __func__ << endl;
       code_ = cod;
       Army(code_);
       cout << " End Derived: " << __func__ << endl;
    }
};

int main ()
{
    ArmyHospital arm1(581);
    return 1;
}

Output:
1
2
3
4
 Base: Default Ctor Army
 Start Derived: ArmyHospital
 Base: Default Ctor Army
 End Derived: ArmyHospital


We can invoke the base class constructor inside derived class but still it will call the base class default constructor first. So, it is called twice. To avoid this, we invoke the parameterised base constructor as an initialiser list in the derived constructor. Moreover, it is better to give precedence to the paramterised constructor. 

 #include <iostream>
using namespace std;

class Army  
{
private:
    int armyCode;
public:
    //Army() { cout << " Base: Default Ctor " << __func__ << endl; };
    Army(int armyCode) { cout << " Base: Parameterised Ctor " << __func__ << endl; };
};

class ArmyHospital : public Army {\
private:
    int code_;
public:
    ArmyHospital(int cod) : Army(cod){
       cout << " Start Derived: " << __func__ << endl;
       code_ = cod;
       cout << " End Derived: " << __func__ << endl;
    }
};

int main ()
{
    ArmyHospital arm1(581);
    return 1;
}

Output:
1
2
3
 Base: Parameterised Ctor Army
 Start Derived: ArmyHospital
 End Derived: ArmyHospital

:Army(cod)  is called constructor initialiser list or member initialiser list of ArmyHospital.

Army(int& armyCode) : armyCode(name) {}               

Army(int& armyCode) {                                                    
    armyCode = name;                                                                                
}

Both are sort of equivalent. They produce generate different machine code to achieve that though produces same result. The first invokes the copy constructor for int.  The second invokes the default constructor for int and then the copy assignment operator, int::operator= .

Array in the initialiser list

#include <iostream>

// User-defined type
class Foo
{
public:
    Foo() : arr{} {}  // Constructor Definition
                      // ": _i(5)" is initialiser list
                      // ": _i(5) {}" is function body
    
    void printValue() const
    {
        for (int i = 0; i < 10; i++) { std::cout << arr[i] << " "; }
        std::cout << std::endl;
    }
private:
    int arr[10];
};


int main()
{

    Foo fooDef;
    fooDef.printValue();

    return 0;
}

Output:

1
0 0 0 0 0 0 0 0 0 0

Click here to debug.

If you have a type that is not default constructible, you can only use the first version.




 So far, I've used constructor initialiser list only for the below: 

  • Base class members' initialisation using constructor.
  • Derived class members' initialisation using the private or protected or public member variables. 
If you still want to initialise the Base class members, you must initialise them in the derived class constructor. Click here to learn with examples.




This post will be updated.

Reference:


Comments