Skip to content

Instantly share code, notes, and snippets.

@leimao
Last active February 27, 2018 22:43
Show Gist options
  • Save leimao/64611cac3d1095259132b0eb2c1494d6 to your computer and use it in GitHub Desktop.
Save leimao/64611cac3d1095259132b0eb2c1494d6 to your computer and use it in GitHub Desktop.
Extremely clear code on unary operator overloading (both prefix and postfix) in C++
// http://www.learncpp.com/cpp-tutorial/97-overloading-the-increment-and-decrement-operators/
class Digit
{
private:
int m_digit;
public:
Digit(int digit=0)
: m_digit(digit)
{
}
Digit& operator++(); // prefix
Digit& operator--(); // prefix
Digit operator++(int); // postfix
Digit operator--(int); // postfix
friend std::ostream& operator<< (std::ostream &out, const Digit &d);
};
Digit& Digit::operator++()
{
// If our number is already at 9, wrap around to 0
if (m_digit == 9)
m_digit = 0;
// otherwise just increment to next number
else
++m_digit;
return *this;
}
Digit& Digit::operator--()
{
// If our number is already at 0, wrap around to 9
if (m_digit == 0)
m_digit = 9;
// otherwise just decrement to next number
else
--m_digit;
return *this;
}
Digit Digit::operator++(int)
{
// Create a temporary variable with our current digit
Digit temp(m_digit);
// Use prefix operator to increment this digit
++(*this); // apply operator
// return temporary result
return temp; // return saved state
}
Digit Digit::operator--(int)
{
// Create a temporary variable with our current digit
Digit temp(m_digit);
// Use prefix operator to decrement this digit
--(*this); // apply operator
// return temporary result
return temp; // return saved state
}
std::ostream& operator<< (std::ostream &out, const Digit &d)
{
out << d.m_digit;
return out;
}
int main()
{
Digit digit(5);
std::cout << digit;
std::cout << ++digit; // calls Digit::operator++();
std::cout << digit++; // calls Digit::operator++(int);
std::cout << digit;
std::cout << --digit; // calls Digit::operator--();
std::cout << digit--; // calls Digit::operator--(int);
std::cout << digit;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment