I think the following member functions operator++ and operator-- code return a reference of type Digit. I think the *this pointer returns the member functions current object. So I guess the Object of type Digit private data member m_nDigit is incremented or decremented and being returned as the data member of returned object. If an object is being returned I don't understand how a reference to object is returned. Could you explain how this computes?
class Digit
{
private:
int m_nDigit;
public:
Digit(int nDigit=0){m_nDigit = nDigit;}
Digit& operator++();
Digit& operator--();
int GetDigit() const { return m_nDigit; }
};
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;
}
Digit& Digit::operator--()
{
// If our number is already at 0, wrap around to 9
if (m_nDigit == 0)
m_nDigit = 9;
// otherwise just decrement to next number
else
--m_nDigit;
return *this;
}