Discussion
Constructors generate initialization code under the covers. Consider:class A {
string s1_, s2_;
public:
A() {s1_ = "Hello, "; s2_ = "world"; }
};
In reality, constructor's code is generated as if you wrote:A() : s1_(), s2_() {s1 = "Hello, "; s2 = "world"; }
That is, the objects not explicitly initialized by you are automatically initialized using their default constructors, and then assigned to using their assignment operators. Most often, the assignment operator of a nontrivial object needs to do slightly more than a constructor because it needs to deal with an already-constructed object.Say what you mean: Initialize member variables in the initializer list, with code that better expresses intent and in addition, as a perk, is usually smaller and faster:A() : s1_("Hello, "), s2_("world") {}
This isn't premature optimization; it's avoiding premature pessimization. (See Item 9)