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.

by Danny Kalev

Jul 16, 2009

Page 1 of 3

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.