Open In App

Constructor Delegation in C++

Last Updated : 11 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In C++, constructor delegation refers to calling a constructor from another constructor within the same class to avoid redundant code. When multiple constructors share the same initialization logic with little difference, it is useful for a constructor to be able to call another constructor of the same class. Constructor Delegation was introduced in C++ 11.

Syntax

The call to another constructor is made in the initializer list:

C++
class GfG {
public:
    GfG(int x, int y) {
        // primary initialization logic
    }

    GfG(int x) : GfG(x, 0) {
        // delegates to above constructor
    }

    GfG() : GfG(0) {
        // delegates to the constructor with one argument
    }
};

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.

Example

Consider the below example where we are a default and a parameterized constructor without delegation:

C++
#include <iostream>
using namespace std;

class GfG {
    int x, y, z;
public:
    GfG() {
        x = 0; y = 0; z = 0;
    }
    GfG (int z) {
        // The below lines are redundant 
        x = 0; y = 0;

        // Only initialize z to new value
        this->z = z;
    }
    void show() {
        cout << x << ' ' << y << ' '
            << z;
    }
};

int main() {
    GfG obj(3);
    obj.show();
    return 0;
}

Output
0 0 3

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 avoid this situation would have been the creation of an init() function that can be called from both the constructors. 

C++
#include <iostream>
using namespace std;

class GfG {
    int x, y, z;

    // init function to initialize x and y
    void init() {
        x = 0; y = 0;
    }

public:
    GfG() {
        init();
        z = 0;
    }
    GfG(int z) {
        init();
        this->z = z;
    }

    void show() {
        cout << x << ' ' << y << ' '
            << z;
    }
};

int main() {
    GfG obj(3);
    obj.show();
    return 0;
}

Output
0 0 3

While the usage of an init() function eliminates duplicate code, it still has its own drawbacks:

  • It’s not quite as readable, as it adds a new function and several new function calls.
  • As 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:

CPP
#include <iostream>
using namespace std;

class GfG {
    int x, y, z;
public:
    GfG() {
        x = 0; y = 0; z = 0;
    }
    
    // Delegating to default constructor
    GfG (int z): GfG() {

        // Only initialize z to new value
        this->z = z;
    }
    void show() {
        cout << x << ' ' << y << ' '
            << z;
    }
};

int main() {
    GfG obj(3);
    obj.show();
    return 0;
}

Output
0 0 3

Important Points

The following rules should be followed while delegating a constructor in C++:

  • Delegating constructor must not form a cycle (direct or indirect).
  • Delegating constructors can only delegate to other constructors of the same class.
  • A constructor can only delegate to one other constructor.
  • A constructor should not initialize other members in its own initializer list after delegation. It means only the call to other constructor should be present in the initializer list.

Constructor Delegation in Inheritance

Constructor delegation is not only useful within a single class but can also be used in inheritance to delegate the initialization to a base class constructor.

Example:

C++
#include <iostream>
using namespace std;

class Base {
public:
    Base(int x) {
        cout << "Base class constructor: "
            << x << endl;
    }
};

class Derived : public Base {
public:

    // Explicit delegation to base class constructor
    Derived(int x) : Base(x) {
        cout << "Derived class constructor"
            << endl;
    }
};

int main() {
    Derived d(1);
    return 0;
}

Output
Base class constructor: 1
Derived class constructor

If no explicit constructor delegation is provided, C++ will implicitly call the default constructor of the base class (if it exists). But for any other constructor, we will have to manually call it.


Practice Tags :

Similar Reads