從http://www.learncpp.com/cpp-tutorial/97-overloading-the-increment-and-decrement-operators/執行前綴操作符++
類聲明
class Digit
{
private:
int m_nDigit;
public:
Digit(int nDigit=0)
{
m_nDigit = nDigit;
}
Digit& operator++();
Digit& operator--();
int GetDigit() const { return m_nDigit; }
};
其實施operator++
Digit& Digit::operator++()
{
// If our number is already at 9, wrap around to 0
if (m_nDigit == 9)
m_nDigit = 0;
// otherwise just increment to next number
else
++m_nDigit;
return *this;
}
我的備用實施operator++
Digit& Digit::operator++()
{
return Digit(m_nDigit == 9 ? 0 : (m_nDigit + 1));
}
我想知道
- 是否有創造一個像我做了一個新的對象的任何缺點,並
- 關於如何選擇這些實現的呢?
您的替代實現看起來像缺少'='。 – 2012-02-26 05:09:39
@CarlNorum:修好了,對不起這個玩笑。 – Lazer 2012-02-26 05:21:26