1

Let's say i'm having a copying contructor

1st way :

Car(Car const& other)
     : Model{other.Model}, Something{other.Something}
{}

2nd way:

Car(Car const& other) 
{
Model = other.Model; 
Something = other.Something; 
}
Kinani
  • 484
  • 1
  • 4
  • 19
  • it did work with : Model{other.Model}, Something{other.Something} – Kinani Aug 02 '14 at 12:03
  • That would compile if he has properly defined `operator=` for Model and Something. The difference is that the initializer list (1st snippet) does initialize the members using a non-default constructor, whereas the other will construct the members using the default constructor and later assign a new value to these members using `operator=`. – jweyrich Aug 02 '14 at 12:03
  • 2
    possible duplicate of [What is benefit of this constructor definition](http://stackoverflow.com/questions/16884000/what-is-benefit-of-this-constructor-definition), [C++: Where to initialize variables in constructor](http://stackoverflow.com/questions/6822422/c-where-to-initialize-variables-in-constructor), and [In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?](http://stackoverflow.com/questions/4589237/in-this-specific-case-is-there-a-difference-between-using-a-member-initializer) – Cody Gray - on strike Aug 02 '14 at 12:05
  • +1 good question, i learned about the `{}` initialization for the first time due to this question. – necromancer Aug 03 '14 at 03:37
  • The question is duplicated : http://stackoverflow.com/questions/4589237/in-this-specific-case-is-there-a-difference-between-using-a-member-initializer – Kinani Aug 04 '14 at 10:07

4 Answers4

5

The first version (Constructor Initializer List) initializes its data members, whereas the second version assigns values to the data members.

If data member is not explicitly initialized in the constructor initializer list, the member is default initialized before the constructor body starts executing. How significant it is depends on the data member type, therefore, it is good practice to favour first version (Constructor Initializer List).

Data members that are const or reference types must be initialized. In addition, class type data members that don't have default constructor must be initialized too.

Alper
  • 12,860
  • 2
  • 31
  • 41
0

The first is calling the copy constructors of the member variables, the second is calling the default constructors of your member variables and then the assignment operator.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

To be correct - to implement special member function (constructor/ copy constructor), because only in that methods can be used member initialization syntax(You have mentioned it as 1-st way). It is used for initialization of objects on creation. Second way is used for assignment of values to already created objects, which can be less efficient (especially for complex objects)

spin_eight
  • 3,925
  • 10
  • 39
  • 61
0

The big difference between the two constructors is that the first form will work even if your Model and Something members are declared const or as & (references). The second form only works if they are not const or references. Also if you use the second form and they have default constructors, those will get run too, so you are essentially constructing them an unnecessary extra time.

For that reason, I generally prefer to use the first form, if reasonably possible.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134