Use Class Member Initializers for Cleaner and Easier-to-Maintain Constructors
Jul 16, 2009, 23:04 (0 Talkback[s])
(Other stories by Danny Kalev)
How to Help Your Business Become an AI Early Adopter
"Normally, 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.
"Problem: Your class has multiple constructors, forcing you to
repeat data member initialization code in every constructor.
"The Solution: Use class member initializers to initialize a
data member only once."