What is the difference between initializing a constructor or doing attribution within the constructor?

Asked

Viewed 607 times

4

What is the difference between the two examples below?

I should also assign the value within the constructor in this class even if I initialized?

example 1:

class sof{
   int teste;
public: 
   sof(int t) : teste(t){}
);

example 2:

class sof{
   int teste;
public: 
   sof(int t){
    teste = t;
   }
);
  • Now you can vote for everything on the site too

2 answers

3


In this example it is indifferent since int does not have a default constructor. Construction is done directly by the compiler in the assignment. It would be different if the member type to be initialized was a type that has a default constructor.

Let’s think of something like this:

class Tipo {
    int x;
public:
    Tipo() {
        x = 0;
    }
    Tipo(int p) {
        x = p;
    }
}

class sof {
    Tipo teste; //chama o construtor padrão
public: 
    sof(int t) {
        teste = Tipo(t); //chama o outro construtor
    }
};

class sof {
    Tipo teste; //não chama nada
public: 
    sof(Tipo t) : teste(t) {} //chama o construtor com parâmetro
};

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that if there is no default constructor, you have no option, the initialization form of member per list (the latter) is required.

Documentation.

Official FAQ.

  • Relatively are initializers independent of what is done within the constructor? Imagine that I have to do some checks inside the constructor, I should not put initializers?

  • Well, that’s another question, but that wouldn’t necessarily be it. You can do it, but then it falls in case you call the default constructor, if that’s what you want, okay. If you prefer to initialize the desired way and then do something again within the constructor is up to you. In most cases you can choose one or the other. There are cases where you do not have the option to leave the default constructor call.

0

I would like to complete the previous answer with the following code::

#include <iostream>

class Tipo {
    int x;
public:
    Tipo() {
        x = 0;
        std::cout << "tipo-default\n";
    }
    Tipo(int p) {
        x = p;
        std::cout << "tipo-int\n";
    }
};

class sof {
    std::ostream& x;
    Tipo teste;
public:
    sof(int t) : x(std::cout << "sof\n") {
        std::cout << "sof-constr\n";
        teste = Tipo(t);
    }
};

int main() {
    sof s(10);
    return 0;
}

Here I show the case cited by the previous answer and, in addition, I show that the constructor of sof is not called in the class scope execution, but just before that constructor is called, as shown in the output:

sof
tipo-default
sof-constr
tipo-int

But it is also possible to see the other case of the usefulness of the Member initializer lists which is the case with std::ostream& x: this must be declared and initialized in the "same statement" for being reference (another case would be variables of type const), so cannot be initialized in the scope of the constructor sof.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.