Suppose I have a CPP class like
class A {
A() :
max_(0),
num_(0),
sum_(0),
sum_squares_(0) {}
void Clear() {
min_ = bucketMapper.LastValue();
max_ = 0;
num_ = 0;
sum_ = 0;
sum_squares_ = 0;
}
void SomethingElse {}
}
I have the initialization code in the constructor and in Clear, is there a way I can avoid having the same code twice.
If you need multiple constructors which call the same logic, you should write all of it within a
ClearorResetfunction and call it from each constructor.Initializer list is REQUIRED in reasonably few cases, and besides those, it is just preferred for efficiency.
It is required for:
constmembers