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


Use Class Member Initializers for Cleaner and Easier-to-Maintain Constructors

A new C++0x feature called a class member initializer allows you to specify a single initializer in the very declaration of a data member. Learn how to use this feature to simplify your constructors' code, reduce potential bugs, and make your code more readable.


ormally, you initialize data members in a constructor's member initialization list. When a class has multiple constructors, each constructor initializes the object's members individually, which often leads to repeating the same initialization code in various places. The use of delegating constructors solves this problem to some extent, but there are cases in which you still need to repeat the initializers, thus risking maintenance difficulties and compromising code readability. This 10-Minute Solution presents a new technique for eliminating repeated initialization code.

Your class has multiple constructors, forcing you to repeat data member initialization code in every constructor.

Use class member initializers to initialize a data member only once.

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