簡體   English   中英

重載必須返回對象引用的運算符

[英]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

}

我在這里有點困惑。 我顯然必須通過引用返回重載增量運算符,但我認為這甚至不可能。 我得到一個內存錯誤,因為我的指針指向的對象消失了。 那么我應該如何重載++運算符?

修改對象本身,然后返回*this

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM