Search

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:

89098

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

5667665

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
Index
9.6 -- Overloading the comparison operators

133 comments to 9.7 — Overloading the increment and decrement operators

  • MoisturBacon

    So I can't get the idea why do we need to create a temporary variable
    "Digit temp(m_digit)" if we can do this:

    I've compared these two methods using timing from lesson 8.16 and the one without a temporary variable is much quicker. Check this out please.
    Am I missing something?

  • MoisturBacon

    So I can't get the idea why do we need to create a temporary variable
    "Digit temp(m_digit)" if we can do:

    I've compared these two methods using timing from lesson 8.16 and the one without a temporary variable is much quicker:

    #include <iostream>
    #include <chrono> // for std::chrono functions

    class Timer
    {
    private:
        using clock_t = std::chrono::high_resolution_clock;
        using second_t = std::chrono::duration<double, std::ratio<1> >;
        std::chrono::time_point<clock_t> m_beg;
    public:
        Timer() : m_beg(clock_t::now())
        {
        }
        void reset()
        {
            m_beg = clock_t::now();
        }

        double elapsed() const
        {
            return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
        }
    };

    class Digit1
    {
    private:
    public:
        int m_digit;
        Digit1(int digit = 0)
            : m_digit(digit) {}
        Digit1 operator--(int); // postfix
        friend std::ostream& operator<< (std::ostream& out, const Digit1& d);
    };

    class Digit2
    {
    private:
    public:
        int m_digit;
        Digit2(int digit = 0)
            : m_digit(digit) {}
        Digit2& operator--(); // prefix
        Digit2 operator--(int); // postfix
        friend std::ostream& operator<< (std::ostream& out, const Digit2& d);
    };

    Digit2& Digit2::operator--()
    {
        --m_digit;
        return *this;
    }

    Digit2 Digit2::operator--(int)
    {    
        Digit2 temp(m_digit);
        --(*this);
        return temp;
    }

    Digit1 Digit1::operator--(int)
    {
        return Digit1((--m_digit) + 1); // return saved state
    }

    std::ostream& operator<< (std::ostream& out, const Digit1& d)
    {
        out << d.m_digit;
        return out;
    }

    std::ostream& operator<< (std::ostream& out, const Digit2& d)
    {
        out << d.m_digit;
        return out;
    }

    int main()
    {
        Timer t;
        Digit1 digit(10000000);
        std::cout << "Decrementing from: " << digit << " to ";
        for (; digit.m_digit > 0; digit--) {}
        std::cout << digit << "\n";
        std::cout << "Time elapsed (WITHOUT temp variable): " << t.elapsed() << " seconds\n\n";
        
        Timer t1;
        Digit2 digit2(10000000);
        std::cout << "Decrementing from: " << digit2 << " to ";
        for (; digit2.m_digit > 0; digit2--) {}
        std::cout << digit2 << "\n";
        std::cout << "Time elapsed (WITH temp variable): " << t1.elapsed() << " seconds\n\n";
            
        return 0;
    }
    [/code]

    • nascardriver

      Measuring time of a debug build doesn't make much sense. If you enable optimizations (the lowest level suffices) there's no considerable difference between the two versions. Both have to create a copy of *this and decrement m_digit. They both do the same, just in a different order.

  • salah

    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.

    Hi,
    here I am returning a local function variable by refrence, and everything is ok..! so how it's still working since it was destroyed ?

  • Ged

    I have a quick question.

    How does it understand that we want - example++ instead of ++example?

    situation1 - ++(int)
    situation2 - (int)++

    By adding a dummy argument int to the function our both functions become different, but how does the compiler know which is which?

  • 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
    Approach-1:

    Version 2:

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

  • Working Code ::Chandra Shekhar

  • Vishs

    Hi,

    For the postfix overloading, you have done:

    Why cant we simply do the below:

    This produces the exact same output!

    • nascardriver

      Hi!

      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

    https://www.google.com/amp/s/blog.cloudflare.com/incident-report-on-memory-leak-caused-by-cloudflare-parser-bug/amp/

  • 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

    Hi,
    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]