vendredi 21 juin 2019

C++: DIfference between default initialization and value initialization for member of class type

In C++11 onwards from what I understand from here and here is that if if I specify a member in the constructor's member initializer list and initialize it with an empty pair of parentheses it gets value-initialized. On the other hand, if I don't initialize a member in the member initializer list it gets default-initialized. If I understand things correctly this difference would matter for an int member, for example, because default initialization would leave it in an indeterminate state. For members of class type I'm having a hard time understanding when default initialization could result in different behavior than value initialization. I see code where people:

  1. initialize class members with empty parentheses in the constructor's member initializer list
  2. don't explicitly initialize class members in the constructor
  3. initialize class members with a default member initializer of {}

Is there ever a difference in effect here or can I just not think about choosing between how I initialize members?

Aucun commentaire:

Enregistrer un commentaire