Search code examples
c++destructorassign

Assignement operator changing value of the assigned object


I implemented a class to handle some external functions (e.g of another DLL). This functions gives me an integer I can use as a handle. Here is the important part of my code:

MyClass
{
public:
    MyClass() { 
        handle = getHandlefromExternalFunction();
    }
    ~MyClass {
        if(handle>0)
            freeHandleFromExternalFunction(handle);
    }
    MyClass& operator=(MyClass& other) {
        freeHandleFromExternalFunction(handle);
        handle = other.handle
        other.handle = 0; //Is this a bad idea?
    }
private:
    int handle;
}

In my main function I have an object of myClass. At some point I am using the assignement operator to change the values of the object:

MyClass object;
//some code
object = MyClass();

After assignement the object created by MyClass() is immediatly destroyed since it gets out of scope. But I don't want freeHandleFromExternalFunction() to be called on that handle, since I am using it in the assigned object. Therefor I change the value of the assigned object in the assignement operator handle = 0. My question is: Is this a bad idea? Has anybody a better solution for my problem?


Solution

  • Yes it's a bad idea. You don't normally expect the right-hand side of an assignment to be modified.

    If you want to move ownership then use the "move" assignment operator together with std::move:

    MyClass& operator=(MyClass&& other) { ... }
    
    // ...
    
    MyClass a = ...;
    MyClass b;
    
    b = std::move(a);
    

    If you only want movement like this (where the can be only one owner of the contained resource), then I also suggest you mark the copy-constructor and copy-assignment operators as deleted:

    MyClass& operator=(MyClass const&) = delete;
    MyClass(MyClass const&) = delete;
    

    And following the rule of five don't forget the move-constructor and destructor:

    ~MyClass() { ... }
    MyClass(MyClass&& other) { ... }