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


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.


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.

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