Passing objects by c++ reference

Asked

Viewed 1,325 times

2

Talk guys, I’m trying to learn a little C++ and I have a question regarding the passage of objects to a class and manipulation of it within this one. I can do this in C#, but I’m trying in c++ without having much success.

Example: c#

class Folha
{

    public void Marcar()
    {
       ...
    }

}

class Papel 
{

    Folha f;

    public Papel(Folha folha_)
    {
        f = folha_;
    }

    public void MarcarFolha()
    {
        f.Marcar();
    }
}

void main()
{

    Folha folha = new Folha();
    Papel papel = new Papel(folha);
    papel.MarcarFolha(); 

}

Is this kind of c+??? If so, how to implement it?

1 answer

3

To do the same in C++ it is common to pass a pointer to the object in the class constructor.

The same example could be encoded like this:

class Folha
{

public:

    void Marcar(){
        cout<<"Marcar de folha";
    }
}; /*<--- ; é necessário em c++*/

class Papel
{
    Folha *f; //guardado como ponteiro

public:

    Papel(Folha *folha_){ //recebe ponteiro
        f = folha_;
    }

    void MarcarFolha()
    {
        f->Marcar(); //acede com -> por ser ponteiro
    }
};

int main()
{
    Folha folha; //cria objeto de forma estatica
    Papel papel(&folha); //passa o seu endereço
    papel.MarcarFolha(); //escreve Marcar de folha

    return 0;
}

See the example in Ideone

Note the differences carefully. Only the word was written once public and all that is from there down is public. The Folha was kept as pointer in class Papel.

Of course I could also do the main with new similar to the one in C#, like this:

void main()
{
    Folha *folha = new Folha(); //ponteiro
    Papel *papel = new Papel(folha); //ponteiro também
    papel->MarcarFolha(); 
}

But this then forces you to release the objects with delete when you don’t need them, which becomes more difficult to manage. For this reason you should give preference to the first form I presented

Using C reference passage++

class Folha
{

public:

    void Marcar(){
        cout<<"Marcar de folha";
    }
}; 

class Papel
{
    Folha f; //guardado sem ser como ponteiro

public:

    Papel(Folha &folha_){ //recebe a referência
        f = folha_;
    }

    void MarcarFolha()
    {
        f.Marcar(); //acede agora com . porque não é ponteiro
    }
};

int main()
{
    Folha folha; //cria objeto de forma estática
    Papel papel(folha); //passa normalmente
    papel.MarcarFolha();

    return 0;
}

Example also in Ideone

Internally the reference is treated as a pointer in the method where it was used. Note however that in the latter case the received sheet in the constructor will be copied to the class field called f, which will be different from folha that had on the main. The same does not happen in the previous examples, and I suspect that this was not the behavior that intended.

  • Beauty, it worked. But so we’re using pointers. We can do the same using "passing by reference". I try to do and always of the error I think not only can to replace the operators "*" by "&", it is not?

  • @guidogd I’ve already edited the answer to contemplate this scenario. See if it answers what you were asking and/or if you have any more questions

Browser other questions tagged

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