class Digit
{
private:
int m_nDigit;
public:
Digit(int nDigit=0)
{
m_nDigit = nDigit;
}
Digit& operator++(); // prefix
Digit& operator--(); // prefix
Digit operator++(int); // postfix
Digit operator--(int); // postfix
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;
}
Digit Digit::operator++(int)
{
// Create a temporary variable with our current digit
Digit cResult(m_nDigit);
// Use prefix operator to increment this digit
++(*this); // apply operator
// return temporary result
return cResult; // return saved state
}
Digit Digit::operator--(int)
{
// Create a temporary variable with our current digit
Digit cResult(m_nDigit);
// Use prefix operator to increment this digit
--(*this); // apply operator
// return temporary result
return cResult; // return saved state
}
int main()
{
Digit cDigit(5);
++cDigit; // calls Digit::operator++();
cDigit++; // calls Digit::operator++(int);
return 0;
}
How can the compiler distinguish the prefix and postfix increment and decrement operators? I understand the dummy variable but
So when the compiler sees ++d it substitutes it for d.operator ++(). When the compiler sees d++ it substitutes it for d.operator ++( 0 ). The argument is not used so in fact the compiler can use any value for the argument. So there is no problem.
When a user wants explicitly to specify the functional call of operator he need also to specify some argument for the postfix operator.
d.operator ++( 0 );
Again the argument is not used in the body of the operator-function. It is used to distinguis this operator from the prefix operator that looks as