How to overload the operator++ in two different ways for postfix a++ and prefix ++a?

Viewed 94426

How to overload the operator++ in two different ways for postfix a++ and prefix ++a?

5 Answers

I know it's late, but I had the same problem and found a simpler solution. Don't get me wrong, this is the same solution as the top one (posted by Martin York). It is just a bit simpler. Just a bit. Here it is:

class Number
{
        public:

              /*prefix*/  
        Number& operator++ ()
        {
            /*Do stuff */
            return *this;
        }

            /*postfix*/
        Number& operator++ (int) 
        {
            ++(*this); //using the prefix operator from before
            return *this;
        }
};

The above solution is a bit simpler because it doesn't use a temporary object in the postfix method.

Related