user2967016
user2967016

Reputation: 133

Overloading an operator that must return a reference to an object

class LongInt
{
public: LongInt& operator++(); //returning a reference
}

LongInt LongInt::operator++()
{
    ...

    LongInt d;


    d.setVector(temp);






    return d; //returning a reference to an object that will be wiped out because it is out of scope

}

I am kinda confused here. I apparently have to return by reference to overload the increment operator, but I don't think that's even possible. I get a memory fault, because the object my pointer is pointing to disappears. So how should I overload the ++ operator?

Upvotes: 0

Views: 71

Answers (1)

Brian Bi
Brian Bi

Reputation: 119069

Modify the object itself, then return *this.

Upvotes: 3

Related Questions