Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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