Skip to content
Related Articles

Related Articles

Improve Article
Constructor Delegation in C++
  • Difficulty Level : Easy
  • Last Updated : 15 Apr, 2018

Sometimes it is useful for a constructor to be able to call another constructor of the same class. This feature, called Constructor Delegation, was introduced in C++ 11.

An example program without delegation :




// A C++ program to demonstrate need of
// constructor delegation.
#include <iostream>
using namespace std;
  
class A {
    int x, y, z;
  
public:
    A()
    {
        x = 0;
        y = 0;
        z = 0;
    }
    A(int z)
    {
        // The below two lines are redundant 
        x = 0;
        y = 0;
  
        /* Only initialize z by passing an argument, 
           while all the other arguments are 
           initialized the same way they were,
           as in the previous constructor*/
        this->z = z;
    }
  
    void show()
    {
        cout << x << '\n'
             << y << '\n'
             << z;
    }
};
  
int main()
{
    A obj(3);
    obj.show();
    return 0;
}
Output:
0
0
3

Solving above redundant code problem using init()
We can see in the above example that the constructors of the above class, despite having different signatures, have first two lines of code common between them, leading to code duplication. One solution to solution to avoid this situation would have been the creation of an init function that can be called from both the constructors.




// Program to demonstrate use of init() to
// avoid redundant code.
#include <iostream>
using namespace std;
  
class A {
    int x, y, z;
  
    // init function to initialize x and y
    void init()
    {
        x = 0;
        y = 0;
    }
  
public:
    A()
    {
        init();
        z = 0;
    }
    A(int z)
    {
        init();
        this->z = z;
    }
  
    void show()
    {
        cout << x << '\n'
             << y << '\n'
             << z;
    }
};
  
int main()
{
    A obj(3);
    obj.show();
    return 0;
}
Output:



0
0
3

Solving above redundant code problem using constructor delegation()
While the usage of an init() function eliminates duplicate code, it still has its own drawbacks. First, it’s not quite as readable, as it adds a new function and several new function calls. Second, because init() is not a constructor, it can be called during the normal program flow, where member variables may already be set and dynamically allocated memory may already be allocated. This means init() needs to be additionally complex in order to handle both the new initialization and re-initialization cases properly.

However, C++ Constructor delegation provides an elegant solution to handle this problem, by allowing us to call a constructor by placing it in the initializer list of other constructors. The following program demonstrates how it is done:




// Program to demonstrate constructor delegation
// in C++
#include <iostream>
using namespace std;
class A {
    int x, y, z;
  
public:
    A()
    {
        x = 0;
        y = 0;
        z = 0;
    }
  
    // Constructor delegation 
    A(int z) : A()
    {
        this->z = z; // Only update z
    }
  
    void show()
    {
        cout << x << '\n'
             << y << '\n'
             << z;
    }
};
int main()
{
    A obj(3);
    obj.show();
    return 0;
}
Output:
0
0
3

It is very important to note that constructor delegation is different from calling a constructor from inside the body of another constructor, which is not recommended because doing so creates another object and initializes it, without doing anything to the object created by the constructor that called it.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :