Browse DevX
Sign up for e-mail newsletters from DevX


Use Delegating Constructors to Eliminate Initialization Code Reduplication-3 : Page 3




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Delegating and Target Constructors
A delegating constructor uses a member initializer list that invokes another constructor (known as the target constructor) of the same class. Once the target constructor returns, the delegating constructor may perform additional initializations:

class Message { char *buff; int msg_id; size_t msg_length; public: //target constructor. Invoked by all other ctors Message() : buff(0),msg_id(0),msg_length(0){}//#1 //the following three ctors invoke the target ctor explicit Message(int) : Message() {} //delegate the initialization to #1 explicit Message(const char *p) : Message() { /* additional code*/ } explicit Message(const A& a) : Message() { /*additional code*/ } };

The new target constructor #1 performs the common initializations. The other three constructors delegate their initialization operations to the target constructor. Once the target constructor has finished, the delegating constructor may execute additional code.

Delegation Chaining
Notice that the target constructor is an ordinary constructor. Therefore, it's possible to create a delegation chain as follows:

struct S { S() {cout<<"first target"<<endl;} #1 S(const char *p): S() {cout<<p<<endl;} #2 S(int n): S("second target") {cout<<"finally, delegating ctor"<<endl;}#3 }; S s1(5);

S has three constructors. When s1 is constructed, constructor #1 executes first, then #2 and finally #3. This output clearly shows the delegation order:

first target second target finally, delegating ctor

Just as with ordinary functions, you need to ensure that the delegation chain doesn't lead to infinite recursion. This might happen if for instance constructor #3 invokes #2 which in turn invokes #3.

Thanks for your registration, follow us on our social networks to keep up-to-date