Previously I had:
MyClass::MyClass() : myMember(VALUE,this,5,"etc.") { ... }
but I need to do some calculation of the values passed to myMember’s constructor. How do I initialise myMember within the constructor body instead? I’m not sure if I should do it like this:
MyMember myMember(calculatedValue,this,5,"etc.");
All member initialisation occurs before the constructor body runs. So if you did this:
it would be assignment, not initialisation. So it won’t work if
MyMembers are not assignable (e.g. they have a private assignment operator). It also won’t work ifmyMemberis declared asconst.You could, however, do something like this:
or even something like this:
Note that I’ve used
statichelper functions here, because using non-staticmember functions before the constructor has completed gets a little weird, in the general case.