Overloading the increment (++
) and decrement (--
) operators are pretty straightforward, with one small exception. There are actually two versions of the increment and decrement operators: a prefix increment and decrement (eg. ++nX; --nY;
) and a postfix increment and decrement (eg. nX++; nY--;
).
Because the increment and decrement operators modify their operands, they’re best overloaded as member functions. We’ll tackle the prefix versions first because they’re the most straightforward.
Overloading prefix increment and decrement
Prefix increment and decrement is overloaded exactly the same as any normal unary operator. We’ll do this one by example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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; } |
Our Digit class holds a number between 0 and 9. We’ve overloaded increment and decrement so they increment/decrement the digit, wrapping around if the digit is incremented/decremented out range.
Note that we return *this. The overloaded increment and decrement operators return a Digit so multiple operators can be “chained” together. Consequently, we need to return an item of type Digit. Since these operators are implemented as member functions, we can just return *this, which is an item of type Digit!
Overloading postfix increment and decrement
Normally, functions can be overloaded when they have the same name but a different number and/or different type of parameters. However, consider the case of the prefix and postfix increment and decrement operators. Both have the same name (eg. operator++), are unary, and take one parameter of the same type. So how it is possible to differentiate the two when overloading?
The answer is that C++ uses a “dummy variable” or “dummy argument” for the postfix operators. This argument is a fake integer parameter that only serves to distinguish the postfix version of increment/decrement from the prefix version. Here is the above Digit class with both prefix and postfix overloads:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
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; } |
There are a few interesting things going on here. First, note that we’ve distinguished the prefix from the postfix operators by providing an integer dummy parameter on the postfix version. Second, because the dummy parameter is not used in the function implementation, we have not even given it a name. This tells the compiler to treat this variable as a placeholder, which means it won’t warn us that we declared a variable but never used it.
Third, note that the prefix and postfix operators do the same job -- they both increment or decrement the class. The difference between the two is in the value they return. The overloaded prefix operators return the class after it has been incremented or decremented. Consequently, overloading these is fairly straightforward. We simply increment or decrement our member variables, and then return *this.
The postfix operators, on the other hand, need to return the state of the class before it is incremented or decremented. This leads to a bit of a conundrum -- if we increment or decrement the class, we won’t be able to return the state of the class before it was incremented or decremented. On the other hand, if we return the state of the class before we increment or decrement it, the increment or decrement will never be called.
The typical way this problem is solved is to use a temporary variable that holds the value of the class before it is incremented or decremented. Then the class itself can be incremented or decremented. And finally, the temporary variable is returned to the caller. In this way, the caller receives a copy of the class before it was incremented or decremented, but the class itself is incremented or decremented. Note that this means the return value of the overloaded operator must be a non-reference, because we can’t return a reference to a local variable that will be destroyed when the function exits. Also note that this means the postfix operators are typically less efficient than the prefix operators because of the added overhead of instantiating a temporary variable and returning by value instead of reference.
Finally, note that we’ve written the post-increment and post-decrement in such a way that it calls the pre-increment and pre-decrement to do most of the work. This cuts down on duplicate code, and makes our class easier to modify in the future.
![]() |
![]() |
![]() |
you have typo, i think ...
in this paragraph
" Note that this means the return value of the overloaded operator [[much]] be a non-reference, because we can’t return a reference to a local variable that will be destroyed when the function exits."
i think you meant must not much, right ?
++cDigit; // calls Digit::operator++();
cDigit++; // calls Digit::operator++(int);
How does the compiler know when to call which function?
because of the dummy parameter (int) in the postfix function, it's the distinction between postfix and prefix.
In the discussion at the bottom of the lesson, it seems to me that you use 'class' in hand-waving style. I'm not sure, but
they both increment or decrement the class
might better read
they both increment or decrement the class instance
or even
they both increment or decrement the class instance member variable
and
The overloaded prefix operators return the class
might be intended to read
The overloaded prefix operators return the class instance
In previous lessons, I don't recall state or classes being returned from functions.
Hope this helps too :-)
Hi , and ASSALAM O ALAIKUM
This is my first comment .
i am feeling happy to be part of LEARNCPP.COM.
:)
i am not able to use
cout << c++ <<
i get the following error:
complex.cpp: In function ‘int main()’:
complex.cpp:83:11: error: no match for ‘operator<<’ in ‘std::cout << c.Complex::operator++(0)’
Second example, line 61: increment should be decrement ;)
I was about to note the same error, and my name is also Mark... Coincidence.... or fate?
First thanks very much for these tutorials. they're cracking.
Just a little proofing moment - in the 2nd to last paragraph "Note that this means the return value of the overloaded operator much be a non-reference" should that be a "must" rather than a "much"?
Cheers
Mark
I have a question.
The way the postfix is implemented... you can't do something like this:
And I think it's because the postfix returns a value, and not a reference, whereas the operator<< receives a reference as parameter, and not a value.
Any thoughts on this? I wanna be able to print a postfix.
Thanks in advance.
Turns out it wasn't the case at all, what happened was that my operator<< recieved the object parameter as not const. It seems that when you pass the object parameter to the operator<< as const, as it should always be, it solves my problem.