When overloading methods in C++, is it possible to consider the equal operator and assign the return value to another class instance?

  c++, operator-overloading, overloading

Is it possible to have in a class a method that modifies its own class instance, but by overloading it can also modify another class instance or even create a new one, if for example the equal sign is used? Or do I need to overload the operator in a special way?

Something like this:

class MyClass{
public:
    MyClass() {};
    MyClass(int x): number(x) {};

    void increaseNumber(){
        number++;
    }

    MyClass increaseNumber() const{
        MyClass tempObj(this->number);
        tempObj.number++
        return tempObj;
    }
private:
    int number = 0;
}


int main(){
    MyClasse a(10);
    
    a.increaseNumber();             // -> a.number == 11
    MyClasse b = a.increaseNumber() // -> b.number == 12   ???
}

Source: Windows Questions C++

LEAVE A COMMENT