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

C++Operator Overloading

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

Should look like this:

class Number 
{
    public:
        Number& operator++ ()     // prefix ++
        {
           // Do work on this.   (increment your object here)
           return *this;
        }

        // You want to make the ++ operator work like the standard operators
        // The simple way to do this is to implement postfix in terms of prefix.
        //
        Number  operator++ (int)  // postfix ++
        {
           Number result(*this);   // make a copy for result
           ++(*this);              // Now use the prefix version to do the work
           return result;          // return the copy (the old) value.
        }
}; 

Solution 2 - C++

The difference lies in what signature you choose for your overload(s) of operator ++.

Cited from the relevant article on this subject in the C++ FAQ (go there for more details):

> class Number { > public: > Number& operator++ (); // prefix ++: no parameter, returns a reference > Number operator++ (int); // postfix ++: dummy parameter, returns a value > };

P.S.: When I found out about this, all I saw initially was the dummy parameter, but the different return types are actually more interesting; they might explain why ++x is considered more efficient than x++ in general.

Solution 3 - C++

You have two ways to overload the two (prefix/postfix) ++ operators for a type T:

##Object method:

This is the easiest way, using "common" OOP idiom.

class T
{
    public :
        T & operator++() // ++A
        {
            // Do increment of "this" value
            return *this ;
        }

        T operator++(int) // A++
        {
           T temp = *this ;
           // Do increment of "this" value
           return temp ;
        }
} ;

##Object non-member function:

This is another way to do this: As long as the functions are in the same namespace as the object they are referring too, they will be considered when the compiler will search for a fonction to handle ++t ; or t++ ; code:

class T
{
    // etc.
} ;


T & operator++(T & p_oRight) // ++A
{
   // Do increment of p_oRight value
   return p_oRight ;
}

T operator++(T & p_oRight, int) // A++
{
   T oCopy ;
   // Copy p_oRight into oCopy
   // Do increment of p_oRight value
   return oCopy ;
}

It is important to remember that, from a C++ viewpoint (including a C++ compiler viewpoint), those non-member functions are still part of T's interface (as long as they are in the same namespace).

There are two potential advantages of the non-member function notation:

  • If you manage to code them without making them friend of T, then you increased the encapsulation of T
  • you can apply this even to classes or structures whose code you don't own. This is a non-intrusive way to enhance the interface of an object without modifying its declaration.

Solution 4 - C++

Declare like so:

class A
{
public:
    A& operator++();    //Prefix (++a)
    A operator++(int); //Postfix (a++)

};

Implement properly - do not mess with what everyone knows they do (increment then use, use then increment).

Solution 5 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrookieView Question on Stackoverflow
Solution 1 - C++Martin YorkView Answer on Stackoverflow
Solution 2 - C++stakx - no longer contributingView Answer on Stackoverflow
Solution 3 - C++paercebalView Answer on Stackoverflow
Solution 4 - C++Kate GregoryView Answer on Stackoverflow
Solution 5 - C++X. MoraView Answer on Stackoverflow