Chaining Constructors in C++
Monday, 18 June 2007
Chain Constructors is one of the refactorings from Refactoring to Patterns (page 340) designed to reduce duplication — in this case duplication between constructors. Unfortunately, it is not such a straight-forward refactoring in C++, since in the current Standard it is not possible for one constructor to delegate to another.
The proposal to the C++ committee to support this feature in the next C++ Standard has been accepted, but the next Standard won't be ready until 2009, with implementations available sometime after that. If you've got a problem in your current project for which this is an appropriate refactoring then two years or more is a bit long too wait. So, with that in mind, I'm posting my work-around here for those that would like this feature now.
Adding a layer of redirection
All problems in Software can be solved by adding a layer of redirection, and this is no exception. In this case, we add a level
of redirection between the class and its data by wrapping the data in a private struct
. Every constructor of
the original class then delegates to the constructor(s) of the internal struct. I'll illustrate with one of the examples
from the Standardization proposal:
class X { struct internal { internal( int, W& ); ~internal(); Y y_; Z z_; } self; public: X(); X( int ); X( W& ); }; X::internal::internal( int i, W& e ): y_(i), z_(e) { /*Common Init*/ } X::X(): self( 42, 3.14 ) { SomePostInitialization(); } X::X( int i ): self( i, 3.14 ) { OtherPostInitialization(); } X::X( W& w ): self( 53, w ) { /* no post-init */ } X x( 21 ); // if the construction of y_ or z_ throws, internal::~internal is invoked
Every constructor of class X
has to initialize the sole data member self
, the constructor of which
encapsulates all the common initialization. Each delegating constructor is then free to do any additional initialization
required.
Within the member functions of X
, all references to member data now have to be prefixed with self.
,
but that's not too bad a price — it makes it clear that this is member data, and is analagous to the use of
this->
, or the m_
prefix.
This simple solution only provides for a single layer of delegation — multiple layers of delegation would require
multiple layers of nested struct
s, but it does provide full support at that level.
pimpl
s and Compilation Firewalls
Once the data has been encapsulated in a private structure, a further step worth considering is a move to the use of a pointer to the internal structure, also known as the pimpl idiom, or the use of a compilation firewall. By so doing, all that is required in the class definition is a forward declaration of the internal class, rather than a full definition. The full definition is then provided in the implementation file for the enclosing class. This eliminates any dependencies on the internal data from other classes, at the cost of forcing the data to be heap allocated. It also removes the possibility of any operations on the enclosing class being inline. For further discussion on the pimpl idiom, see Herb Sutter's Guru of the Week entry.
Refactoring steps
Here's a quick summary of the steps needed to perform this refactoring:
- Create a private
struct
namedinternal
in the classX
being refactored with an identical set of data members to classX
. - Create a data member in class
X
of typeinternal
namedself
, and remove all other data members. - For each constructor of
X
, write a constructor ofinternal
that mirrors the member-initializer list, and replace the member initializer list of that constructor with a single initialization ofself
that forwards the appropriate constructor parameters. - Replace every reference to a data member
y
of classX
to a reference toself.y
. - Eliminate duplication.
Posted by Anthony Williams
[/ cplusplus /] permanent link
Stumble It! | Submit to Reddit | Submit to DZone
If you liked this post, why not subscribe to the RSS feed or Follow me on Twitter? You can also subscribe to this blog by email using the form on the left.
Design and Content Copyright © 2005-2024 Just Software Solutions Ltd. All rights reserved. | Privacy Policy
No Comments