9.7 — Overloading the increment and decrement operators

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 (e.g. ++x; --y;) and a postfix increment and decrement (e.g. x++; y--;).

Because the increment and decrement operators are both unary operators and they 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:

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.

This example prints:


Note that we return *this. The overloaded increment and decrement operators return the current implicit object so multiple operators can be “chained” together.

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:

This prints


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 object. The difference between the two is in the value they return. The overloaded prefix operators return the object 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 object before it is incremented or decremented. This leads to a bit of a conundrum -- if we increment or decrement the object, we won’t be able to return the state of the object before it was incremented or decremented. On the other hand, if we return the state of the object 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 object before it is incremented or decremented. Then the object 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 object before it was incremented or decremented, but the object 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.

9.8 -- Overloading the subscript operator
9.6 -- Overloading the comparison operators

125 comments to 9.7 — Overloading the increment and decrement operators

  • I have a query about postfix increment/decrement operator.
    Kindly tell why we write it as
        Digit &Digit::operator++(int)
    instea of
        Digit &Digit::operator++( )
    mean why we use 'int'?
    kindly explain.

    • nascardriver

      It's been defined like that. The overload without parameters is the prefix operater, the overload with an argument is the postfix operator. There is no argument to this function. I guess this was the easiest way of differentiating them without making changes to the language.

  • Hi Alex ,

    Just wanted to clear a doubt .I have tried to overload the pre-increment operator in two version.I am not sure what is wrong with the Second version.
    Kindly throw some light over this.

    I have defined a Digit class having two datamembers a and b

    Version 2:

    Approach 2 is working fine but Approah 1 is not working while overloading pre-increment ++ operator

  • Working Code ::Chandra Shekhar

  • Vishs


    For the postfix overloading, you have done:

    Why cant we simply do the below:

    This produces the exact same output!

    • nascardriver


      Your suggestion works for the `Digit` class, so yes, we could do it. As soon as there's more than one member in the class, your suggestion no longer works and would have to be changed to what is shown in the lesson.

  • Eru

    Hello, I've asked a question in chapter 9.3 about chaining operators earlier, so I'm still trying to wrap my head around them.

    I'm guessing that the following code

    would be considered as chaining, and this works because we're returning the val by reference, so each returned val after the operator-- function is the same val we've started with.(if I'm correct with my assumption here)

    Now by this logic, the post-fix increment or decrement should not work for chaining(val----) and if I haven't done any mistakes while testing, it indeed does not work. (Because the value returned from the operator--(int x) is not the val we've started with, it's just a copy with a different address)

    Also by doesn't work i mean it still gets evaluated correctly to the value, but it(the chained operators) does not affect the original value, as in:

    However the pre-fix version works correctly, as in;

    I would be glad if anyone can answer whether what I wrote is correct or not, the whole thing is too complicated! :D

  • Paulo Filipe

    Guys, here's a situation where a simple mistake of using prefix vs postfix caused a major memory leak in CloudFlare. Loads of private data exposed because of a simple

    instead of

  • Atas

    How does the syntax work out when we call the postfix version of ++? Where does the dummy variable come from to tell the compiler we want the postfix ++? How does compiler know to look to the left of operator++ to find the object, if all other unary operators have the object to the right? Thanks!

  • lyf

    is temp will be destroyed after function exits? maybe should use new Digit ?

  • hassan magaji

    i took @Alex's code and modified it a bit, instead of instantiating two objects of Digit, its now one:

    could this enhance performance?

  • You have

    Remove the '&'. If your code causes errors, please post your code. An error message alone is rarely informative enough to help.

  • Chandra

    Just had a query :

    For overloading Post incr/decr operator here we used Digit operator++(int) but for pre increment operator overloading we have used Digit& operator().

    My question is why the return types are different ie Digit and Digit&.

    Kindly help with the explanation.

    Kind Regards,
    A C++ Seeker :)

  • Chandra

    While Compiling Giving Warning:
    |warning: reference to local variable 'temp' returned [-Wreturn-local-addr]

    Also in the output screen not able to get anything

Leave a Comment

Put all code inside code tags: [code]your code here[/code]