I have this class that has many class members, and a lot of different constructors.
Until now, I used a constructor initialization list in each of the constructors that I have, tuning each member the way I wanted.
This is quite tedious, because everytime I add a new member to my class, I have to visit each constructor and update the initialization list to add a default value to this member.
So, I thought I would add a method to initialize the values I need. Problem! Since the method is executed after the initialization list, the particular values I put in this init list are overriden by my method.
Quick exemple:
class A
{
public:
A();
A( B b );
A( int i );
// A( .... ); plenty of them
private:
int member1, m2, m3,m4;
bool b1,b2, b3;
// ....
// every time I add a member I have to modify the initialization lists
// solution: agregate member initialization in a member function:
void init_members();
}
// init list constructors
A::A() : m1(false), m2(false), m3(false), m4(true) .... // looong list
{
}
A::A( B b) : m1(b.state()), m2(false), m3(false), ... // loong list
{
}
// problem, if I use init_members:
void A::init_members()
{
m1 = false;
m2 = false;
m3 = false;
// ...
}
A::A( int i ) : m1( true)
{
init_members(); // overrides m1 !!!
}
So, my question: can I mix list initializer and method initializers, so that list initializers have precedence over the method initializer?
In my example above, I want m1 to stay true
for the last constructor.
Note: I know I could move the initialization list after the method call, but this means I'd assign twice the values to the members : once in init_members()
, then overriding it in the constructor. Not optimal enough :-)
I was hoping for some little trick, if you have that in stock.