Delegating constructors (C++11)

Note: IBM supports selected features of C++11, known as C++0x before its ratification. IBM will continue to develop and implement the features of this standard. The implementation of the language level is based on IBM's interpretation of the standard. Until IBM's implementation of all the C++11 features is complete, including the support of a new C++11 standard library, the implementation may change from release to release. IBM makes no attempt to maintain compatibility, in source, binary, or listings and other compiler interfaces, with earlier releases of IBM's implementation of the new C++11 features.

Before C++11, common initializations in multiple constructors of the same class could not be concentrated in one place in a robust, maintainable manner. To partially alleviate this problem in the existing C++ programs, you could use assignment instead of initialization or add a common initialization function.

With the delegating constructors feature, you can concentrate common initializations and post initializations in one constructor named target constructor. Delegating constructors can call the target constructor to do the initialization. A delegating constructor can also be used as the target constructor of one or more delegating constructors. You can use this feature to make programs more readable and maintainable.

Delegating constructors and target constructors present the same interface as other constructors. Target constructors do not need special handling to become the target of a delegating constructor. They are selected by overload resolution or template argument deduction. After the target constructor completes execution, the delegating constructor gets control back.

In the following example, X(int x, int y) and X(int x, int y, int z) both delegate to X(int x). This example demonstrates a typical usage of placing common initializations in a single constructor.
#include <cstdio>

struct X {
   const int i;
   X(int x, int y) : X(x+y) { }
   X(int x, int y, int z) : X(x*y*z) {}
   X(int x) : i(x) { }
};

int main(void){
   X var1(55,11);
   X var2(2,4,6);
   std::printf("%d, %d\n", var1.i, var2.i);
   return 0;
} 
The output of the example is:
66,48
A delegating constructor can be a target constructor of another delegating constructor, thus forming a delegating chain. The first constructor invoked in the construction of an object is called principal constructor. A constructor cannot delegate to itself directly or indirectly. The compiler can detect this violation if the constructors involved in a recursive chain of delegation are all defined in one translation unit. Consider the following example:
struct A{
  int x,y;
  A():A(42){}
  A(int x_):A() {x = x_;}
};
In the example, there is an infinitely recursive cycle that constructor A() delegates to constructor A(int x_), and A(int x_) also delegates to A(). The compiler issues an error to indicate the violation.
You can use the delegating constructors feature interacting with other existing techniques:
  • When several constructors have the same name, name and overload resolution can determine which constructor is the target constructor.
  • When using delegating constructors in a template class, the deduction of template parameters works normally.