简体   繁体   中英

Constructor parameters C++ const and &

I can't get the parameters of the constructor right. It's keep poping the same error over and over. The arguments of the class can't be changed i can only change the constructor.

Error C2789 'Aluno::numero': an object of const-qualified type must be initialized

Error C2530 'Aluno::escola': references must be initialized

class Aluno {
    string nome;
    string& escola;
    const int numero;
public:
    Aluno(string nome, string escola, int numero){
        this->nome = nome;
        this->escola = escola;
        this->numero = numero;
    }
};

int main()
{
    Aluno * a = new Aluno("aaa", "bbb", 123);

    return 0;
}

Class members that are const or references MUST be initialized in a constructor's member initialization list , eg:

class Aluno
{
    string nome;
    string& escola;
    const int numero;
public:
    Aluno(string nome, string &escola, int numero)
        : nome(nome), escola(escola), numero(numero)
    {
    }
};

int main()
{
    string b = "bbb";
    Aluno * a = new Aluno("aaa", b, 123);
    delete a; 
    return 0;
}

However, notice that I had to change your escola input parameter. You are passing that parameter by value , but are trying to store a reference to it. The escola class member would be bound to a string object that is local to the constructor and goes out of scope when the constructor exits, leaving the reference dangling. You don't want that.

So, either the escola input parameter must be changed to a reference as well, as shown above, or else the escola class member can't be declared as a reference anymore, eg:

class Aluno
{
    string nome;
    string escola;
    const int numero;
public:
    Aluno(string nome, string escola, int numero)
        : nome(nome), escola(escola), numero(numero)
    {
    }
};

int main()
{
    Aluno * a = new Aluno("aaa", "bbb", 123);
    delete a; 
    return 0;
}

For const members, you need to use an initializer list:

Aluno(string nome, string escola, int numero)
    : numero(numero) {
    // ...
}

You also need to initialize references in the same way, but this requires passing a reference as the parameter:

Aluno(string nome, string& escola, int numero)
    : escola(escola), numero(numero) {
    // ...
}

Even though it isn't required by the compiler, initializing nome in the same way is more efficient.

Aluno(string nome, string& escola, int numero)
    : nome(nome), escola(escola), numero(numero) {
}

To initialize members you need to use either in-class initialization or the constructor initialization list. Not the constructor body.

In the constructor body you may re-assign values to members, but at that point they have already been default initialized (unless you explicitly initialized them in-class or by using the initialization list) and not all types can be default initialized (like references).

escola is a reference, so it needs to be initialized in the field initialization list as it's being allocated. The same is true for your const numero :

Aluno(string nome, string escola, int numero) : 
    nome(nome), escola(escola), numero(numero) { }

However, this would result in UB since the parameter escola that your member escola is referencing will go out of scope. As such, the parameter must also be a reference:

Aluno(string nome, string &escola, int numero) : 
    nome(nome), escola(escola), numero(numero) { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM