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

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

ere's a common scenario: a base class has multiple constructors, each of which takes a different number of parameters of various types. A derived class has to declare "mirror constructors" with signatures that correspond to the base class constructors. Each mirror constructor merely forwards its arguments to the matching base constructor.

Defining the mirror constructors manually is a laborious and error-prone task, because you must update them whenever the base class's constructors change. Fortunately, a new C++0x feature called inheriting constructors lets you delegate that task to the compiler.

This 10-Minute Solution shows how inheriting constructors can make your code more secure, easier to maintain, and more readable.



When deriving a class from a base class, you need to define constructors that mirror those of the base class.

Rather than defining the mirror constructors manually, let the compiler generate them using the inheriting constructors feature.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap