Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Use C++0x's Inheriting Constructors to Reduce Boilerplate Code in Class Hierarchies-2 : Page 2

A base class with multiple constructors forces a derived class to define corresponding constructors that merely forward their arguments to the matching base constructor. In C++03, you define the derived class's constructors manually. Learn how C++0x's inheriting constructors feature automates this process, thereby simplifying code maintenance and making your code more readable and secure.


advertisement

Exemplifying Mirror Constructors

Today, initializing a derived class with exactly the same set of constructors as its base class means you have to write a set of constructors (known as mirror constructors) in the derived class. The sole purpose of those mirror constructors is to forward their arguments to the matching constructor of the base class. This is tedious and causes maintenance problems, as the following listing demonstrates:

struct A1 { explicit A1(int i); explicit A1(double d); }; struct D1 : A1 { //manual definitions of mirror ctors explicit D1(int n): A1(n){} explicit D1(double d): A1(d){} private: int x; };

Author's Note: The informal term "mirror constructor" refers to a user-defined constructor in a derived class that merely forwards its arguments to a base class constructor with a matching signature.



It's bad enough to have to do this once, but if you derive another class from D1, there's no escape from repeating this boilerplate code yet again:

struct E1 : D1 { //manual definitions of mirror ctors in the derived class explicit E1(int n): D1(n){} explicit E1(double d): D1(d){} private: int y; };

There's nothing special about mirror constructors; therefore, delegating their declarations to the compiler will not only make your job easier; it will also make your code more readable, because you need only a single declaration to generate all of the mirror constructors. As a bonus, the automatically generated code will probably be more efficient than handwritten code. The C++0x inheriting constructors feature solves this problem.

The solution lies in a special type of using declaration that names the base class constructor in the derived class. This using declaration implicitly declares a set of inheriting constructors in the derived class. Inheriting constructors are similar to mirror constructors except that the compiler declares them rather than the programmer. As with other implicitly-declared constructors, inheriting constructors are defined implicitly only if they are used in the program. Here's a modified version of D1 with inheriting constructors:

struct D1 : A1 { using A1::A1; //inheriting ctors declaration private: int x; };

The using declaration in the preceding code is what makes this work. The line using A1::A1; instructs the compiler to implicitly declare two inheriting constructors in D1 whose signatures, explicitness, access, exception specifications, and presence or absence of constexpr and =delete are identical to those of A1's two constructors. The inheriting constructors forward their arguments to the base class constructors using rvalue references. Notice that an implicitly defined inheriting constructor doesn't initialize user-declared data members of the derived class; such data members undergo default initialization, as do all data members that have no explicit initializer:

D1 d(10); //OK. d.x has an indeterminate value

The definition of d causes the compiler to implicitly define the inheriting constructor D1(int) that was already implicitly-declared along with D1(double) when the compiler encountered the using A1::A1; declaration inside D1.

The implicitly defined inheriting constructor D1(int) forwards its argument to the matching B1(int) constructor. Similarly, the following code causes the compiler to implicitly define the D1(double) constructor, which forwards the argument 8.99 to A1(double), leaving d.x with no explicit initializer.

D1 d2(8.99); //OK, d.x isn't initialized

Lastly, the following line is ill-formed because D1 has no default constructor:

D1 d3; //error



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap