What’s the difference between initializing a variable in these constructs? And how do you set a constructor to default?

Asked

Viewed 371 times

1

Is there a difference when initializing a variable in any of these constructor forms? And how do I default a constructor (default) in a class that has more than one constructor?

Constructor 1:

class Teste
{
private:
    int valor1, valor2;
    float valor3;
public:
    Teste(void) : valor1(0), valor2(0), valor3(0.f) {}
};

Constructor 2:

class Teste
{
private:
     int valor1, valor2;
     float valor3;
public:
     Teste(void)
     { 
         valor1 = 0;
         valor2 = 0;
         valor3 = 0.f;
     }
};

Currently I prefer to use the method of constructor 1 because I find cleaner, changes in something?

  • Related: https://answall.com/q/248436/64969

2 answers

2


In this specific case nothing changes. I recommend the first only to leave uniform with the cases that need to do so.

In cases of having members that need to be initialized and are classes there will be creation of a temporary object of the value and then copied to the variable and this is inefficient. Worsens if the member type has a default constructor.

If a member is declared const the only way to initialize it is like in the first example, using startup list (the name of this).

FAQ.

The default constructor is a constructor without parameters.

You don’t have to use void to empty the parameters in C++.

  • Just a doubt, the first case has the question of order? (or it depends on the compiler or default you use, or configuration, I do not remember, I’ve been a little rusty) , for example the error will be initialized after, sorry if I’m talking nonsense.

  • @Did Guillhermenascimento see the following items from the FAQ? He speaks precisely of this.

  • Now that I’ve noticed, thank you, I’ll read.

  • There is no way I can choose which default constructor I want in C++? Example: https://paste.ofcode.org/37rwCgbj4CpENCGmSiBPAb

  • Okay, so is he the standard? What do you want to gain from this?

  • It would make the use of the class easier, for example I am developing a library and I want to leave it open source, if I leave the most used constructor for the standard form class would make it easier to use and understand it (class)so I would know which arguments right away without having to go in class to see the constructor arguments I need.

  • I can’t see what’s in it for me. In fact I can’t even understand what "most used constructor for the standard form class" means. I think you’re trying to create a solution to a problem that doesn’t exist.

Show 2 more comments

0

The constructor can assign values to its members in two ways:

  1. You can accept values as parameters and assign them to variables within the function body constructor, as shown in the following example:

     #include <iostream>
     using namespace std;
    
     class Animais {
       private:
           string nome;
           int idade;
    
       public:
           Animais(string n, int i) {
               this->nome = n; this->idade = i;
           }
           int getIdade();
           string getNome();
     };
    
     string Animais :: getNome() {
         return this->nome;
     }
    
    
     int Animais :: getIdade() {
         return this->idade;
     } 
    
    
    int main()
    {
       Animais armazenar("Ralf", 8);
       cout << "Nome do animal: " << armazenar.getNome() << endl;
       cout << "Idade do animal: " << armazenar.getIdade() << endl;
       return 0;
    }
    
  2. A startup list can be used before the body of the function. For example:

    #include <iostream>
    using namespace std;
    
    class Animais {
       private:
          string nome;
          int idade;
    
       public:
          Animais(string n, int i) : nome(n), idade(i) {}
          int getIdade();
          string getNome();
     };
    
    string Animais :: getNome() {
      return this->nome;
    }
    
    
    int Animais :: getIdade() {
      return this->idade;
    } 
    
    
    int main()
    {
      Animais armazenar("Ralf", 8);
      cout << "Nome do animal: " << armazenar.getNome() << endl;
      cout << "Idade do animal: " << armazenar.getIdade() << endl;
      return 0;
    }
    

Browser other questions tagged

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