I am having problems with the overloading operator functions return value the *this value why does it not return the this value without the indirection wouldnt the this value return the address of the object? Im confused with the derenferencing value *this being used instead.
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;
}
this is a pointer - a pointer to Digit (pointer to optionally cv-qualified Digit)
*this is a reference - a reference to Digit (reference to optionally cv-qualified Digit).
When we dereference (use the unary * operator on) a pointer to T, we get a reference to T.
The overloaded operators return the reference obtained by derefencing this - they return *this.