Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to reassign *this inside a class' method?

I have an object that is related to some file stored on the disk. The object's constructor accepts this file as an argument, reads it and creates an actual object with the settings depending on the file's content. During the runtime there is a probability for this file to be modified by the user. The object has a method that checks if the file has been modified since the last read, and if this is true, the object has to be updated. There are enough members to be updated inside the object, so instead of manually update each of them it is more convenient to simply create a new object and move it into the existing one(less typing and better readability). But is it actually safe to change *this during the method execution like in the example below?

void Object::update()
{
    if (this->isModified(file)) // Check if a file has been modified since the last read
    {
        try
        {
            Object newObject(file); // May throw
            *this = std::move(newObject); // Is it safe?
        }
        catch (const std::exception& ex)
        {
            // Invalidate and rethrow exception
            this->invalidate();
            throw(ex);
        }
    }
    // ...
}
like image 242
Alexey104 Avatar asked Dec 18 '21 09:12

Alexey104


People also ask

What are the class methods*?

A class method is a method that is bound to a class rather than its object. It doesn't require creation of a class instance, much like staticmethod. The difference between a static method and a class method is: Static method knows nothing about the class and just deals with the parameters.

Why do we need class method in Python?

A class method is a method which is bound to the class and not the object of the class. They have the access to the state of the class as it takes a class parameter that points to the class and not the object instance. It can modify a class state that would apply across all the instances of the class.


Video Answer


1 Answers

You seem to be worried about this appearing on the left hand side, though *this = ... merely calls operator=. Typically the assignment operator that takes a rvalue reference just moves the members.

Consider this simpler example:

struct foo {
    int x = 42;
    foo& operator=(foo&& other){
        x = std::move(other.x);
        return *this;
    }
    void bar(){
        foo other;
        operator=(std::move(other));
    }
};

Nothing wrong with that.

Another way to convince yourself that *this = std::move(newObject); is not problematic is to inline all code from the assignment operator to update. For the example above that would be:

struct foo {
    int x = 42;
    void bar(){
        foo other;
        x = std::move(other.x);
    }
};
like image 159
463035818_is_not_a_number Avatar answered Oct 12 '22 17:10

463035818_is_not_a_number