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 :
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.
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:
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Constructor Overloading in C++
- Copy Constructor in C++
- Can a constructor be private in C++ ?
- What is conversion constructor in C++?
- How to create a List with Constructor in C++ STL
- Difference Between Constructor and Destructor in C++
- Dynamic Constructor in C++ with Examples
- When is copy constructor called?
- When should we write our own copy constructor?
- Advanced C++ | Virtual Constructor
- Passing a vector to constructor in C++
- Copy constructor vs assignment operator in C++
- Is body of a Default Constructor blank in C++?
- Order of Constructor/ Destructor Call in C++
- Is it possible to call constructor and destructor explicitly?
- Why copy constructor argument should be const in C++?
- Advanced C++ | Virtual Copy Constructor
- C++ default constructor | Built-in types
- Does C++ compiler create default constructor when we write our own?
- Calling virtual methods in constructor/destructor in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.