C++ - Delegating Constructors
C++ Delegating Constructors
Delegating constructors is a feature that simplifies the way constructors in a class handle the initialization task. It makes it easier to maintain object initialization by reducing redundancy, and by allowing one constructor to call another in the same class.
Use of Delegating Constructors
In class design, multiple constructors are often used to handle different initialization scenarios. However, this can lead to repetitive code because each constructor may duplicate similar initialization logic.
By using the delegating constructors, code redundancy can be avoided. A single "main" constructor can handle most initialization tasks, while other constructors delegate to it.
This approach follows the DRY (Don't Repeat Yourself) principle and makes the code easier to maintain.
Syntax of Delegating Constructors
The syntax for a delegating constructor involves calling another constructor in the initializer list.
class Example {
public:
// Primary constructor
Example(int value) : data(value) {}
// Delegating constructor
Example() : Example(0) {}
private:
int data;
};
Where,
- The Example(int value) is the main constructor that does the real initialization.
- Example() is a delegating constructor that calls Example(int value) with a default value of 0.
Rules for Using Delegating Constructors
The following rules are applied when using delegating constructors in C++11 and later:
- A constructor can delegate to only one other constructor in C++11 and later.
- Delegation must occur within the same class.
- Circular delegation (like A() : A(x) and A(x) : A()) is prohibited and will result in a compile-time error.
Example of Delegating Constructors
Here is a basic simple program for delegating constructors:
#include <string>
#include <iostream>
class Student {
public:
// Primary constructor
Student(const std::string& name, int age, double grade)
: name(name), age(age), grade(grade) {}
// Delegating constructor with default grade
Student(const std::string& name, int age)
: Student(name, age, 0.0) {}
// Delegating constructor with default age and grade
Student(const std::string& name)
: Student(name, 18, 0.0) {}
void display() const {
std::cout << "Name: " << name
<< ", Age: " << age
<< ", Grade: " << grade << "\n";
}
private:
std::string name;
int age;
double grade;
};
int main() {
Student s1("Navya", 20, 90.5);
Student s2("Divya", 22);
Student s3("Kavya");
s1.display();
s2.display();
s3.display();
return 0;
}
Output
Name: Navya, Age: 20, Grade: 90.5 Name: Divya, Age: 22, Grade: 0 Name: Kavya, Age: 18, Grade: 0
Advantages of Delegating Constructors
The following are the advantages of using delegating constructors:
- Centralized Initialization − By consolidating initialization logic into a single constructor, your code becomes easier to read and maintain
- Avoidance of Redundancy − Reusing constructor logic eliminates duplicated code.
- Ease of Modification − Changes to initialization logic need to be made only in the primary constructor.