What is the purpose of a delegating constructor introduced in modern C++?

Options

  • A. One constructor calls another constructor of the same class
  • B. A constructor calls a destructor of another class
  • C. It prevents object creation
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. One constructor calls another constructor of the same class

Detailed Explanation

Answer: (A) One constructor calls another constructor of the same class

A delegating constructor allows one constructor of a class to call another constructor of the same class.

This feature helps avoid duplicate initialization code.

Example:

class Test {
int x;
public:
Test() : Test(10) { }
Test(int n) : x(n) { }
};

Here, the no-argument constructor delegates the actual initialization to the constructor taking an integer.