Should I call the base class default constructor in the initialization list?
The base class constructor is called in both cases.
Here is a link to an article with more info.
If B
doesn't have a user declared constructor, the behavior is different. Compare:
struct SimpleAggregate {
int a;
float b;
};
struct ClassWrapper : SimpleAggregate {
ClassWrapper() : SimpleAggregate() { }
};
ClassWrapper w;
Now, w.a
and w.b
are guaranteed to be zero. If you would have left off the explicit initialization of the base class, they would have indeterminate values.
It may be unknown to you that, despite the syntax, the above use of SimpleAggregate()
does not call the default constructor, though. It simply value initializes the base class (we have several good answers here on Stackoverflow about what "value initialization" is), not calling the default constructor because there is none user declared.