dcsimg
LinkedIn
Google+
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

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.


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

WEBINAR:

On-Demand

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


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
Thanks for your registration, follow us on our social networks to keep up-to-date