Difference between the constructors of C ++?


Hi, I just saw a video about c ++ but it's in English, there was a part where I started explaining about POO, and he talked about the builders, I understand what it is, but he did two of which I do not know his differences.

This is the first type of constructor:

Jugador::Jugador (int edad, int vida) {
    edad = edad;
    vida = vida;

This the second:

Jugador::Jugador (int edad, int vida) : edad(edad), vida(vida) {}

What is the difference between the two?

asked by Shum 14.01.2018 в 06:06

2 answers


There is a difference HUGE . Maybe it does not appreciate very well because of the type used ( int ); I propose this other example:

#include <iostream>

using namespace std;

struct Tipo {
  Tipo( ) { cout << "Constructor por defecto\n"; }
  Tipo &operator=( int x ) { cout << "Asinacion\n de " << x << "\n"; return *this; }

struct Jugador {
  Tipo vida;

  Jugador( ) : vida( ) { }
  Jugador( int x ) { vida = x; }

int main( ) {
  Jugador j1;      //: vida( ) { }
  Jugador j2( 5 ); // { vida = 5; }

  return 0;

You will see that I do the same as you indicate (but changing the order): we call the version : XXX first, and then we use the asinación.

How many lines will be printed?


Default constructor
  Default constructor

  • When doing Jugador( ) : vida( XXX ) { } , you are calling the indicated constructor of Tipo
  • When doing Jugador( ) { vida = 5; } , you are making 2 calls :
  • To the default constructor of Tipo (even if you do not see it, the compiler does it for you).
  • To the assignment operator.

In other words: the expression Jugador( ) : vida( ) { ... ALWAYS is used; well explicit by us, well implied by the compiler. And, as a consequence, it is impossible to leave member-variables without initializing.

Keeping in mind the above, it is better, whenever possible, to use the initialization list : var( XX ), var2( XX ), ... { . We save calls to assignment functions, and the call to the builders will be done anyway (like it or not).

Note: there are tricks to leave members uninitialized if necessary, but it goes beyond the scope of the question.

answered by 14.01.2018 в 08:54

Semantically no difference, is the same constructor implemented in different ways. The two expressions have the same statement:

Jugador::Jugador(int edad, int vida);

So, the difference is in the implementation of the same constructor; the first is functionally equivalent to the second, only the second is (probably) more efficient.

The second form uses what is called "initializer list" or initialization list. The form that is preferred today is:

Jugador::Jugador(int edad, int vida) : edad{edad}, vida{vida} {}

(you will notice that braces are used instead of parentheses, which is what is called uniform initialization ).

As you see here, more than one theme is involved. I said that using a initializer list can be more efficient, for example, although a current compiler probably generates the same instructions for both.

As a recommendation, I would tell you that the "normal", "expected", "desirable" way to write that constructor is using initializer list along with uniform initialization .

answered by 14.01.2018 в 17:27