mercredi 19 février 2020

Is it possible to dereference an object to another object inside his own method? [closed]

I have a question about some C++ problem. Let's say I have a really big class called BigClass and I say BigClass* foo = new BigClass();

Let's say I have a method inside BigClass that defines another local object tmp of type BigClass and at the end of the method, I check if there was the error by doing some stuff with tmp and if not I want to dereference this to that object. I don't want to copy all the arguments from tmp to this, because the object is too big and the process would be slow. Is there any way to dereference the object inside its own method?

Example:

class BigClass
{
    int a;
    long b;
    std::vector<int> c;
    // other members...

    void replace()
    {
        BigClass* tmp = new BigClass();
        // do some calculations with tmp
        *this = tmp; // problem
    }
}

Why is *this = tmp a problem?

I defined an operator= that just does something like this:

this.a = src.a;
this.b = src.b;
this.c = src.c;
.
.
.

But there I make a copy of all member variables and that is slow. I want to simply dereference the object instead of copying all member variables.

Aucun commentaire:

Enregistrer un commentaire