I have been working in C++ for some time now, but I am unsure about the difference between the two options:
public : Thing(int _foo, int _bar): member1(_foo), member2(_bar){}
and
public : Thing(int _foo, int _bar){
member1 = _foo;
member2 = _bar;
}
I have a feeling that they do the same thing, but is there a practical difference between these 2 syntaxes. Is one of these safer then the other, and do they handle default parameters differently.
Not totally accustomed to the first example so if I made a mistake on it I apologize.
They are not the same if
member1andmember2are non-POD (i.e. non-Plain Old Data) types:is equivalent to
because they will be initialized before the constructor body starts executing, so basically twice the work is done. That also means, if the type of these members don’t have default constructor, then your code will not compile.