I’ve got a couple of constructors for my class:
MyClass();
MyClass( int param1 );
MyClass( int param1, int param2 );
MyClass( std::string otherParam );
MyClass( std::string otherParam, int param1 );
MyClass( std::string otherParam, int param1, int param2 );
Now, there is some argument checking to be done in the constructor, e.g. -3 < param1 < 3. What’s the preferred way of doing this check? Should I call checkParam1() and checkOtherParam() from each constructor?
In C++11 you can use a delegated constructor feature:
The constructor with fewer parameters calls the constructor with more parameters, and the constructor with the most parameters does all the checking.
Note that you should be able to unify the last three constructors by adding default values for
param1andparam2, like this: