-1

I'm having trouble with the unary ++ overloaded operator.

Here is my code...

 #include<iostream>

 using namespace std;

 class Index{

      int value;
 public:
     Index() : value(0) { }
     int GetIndex() const
     {
        return value;
     }
     void operator ++()
     {
        value++;
     }
};
int main()
{   
   Index idx1,idx2;

   ++idx1;
   idx2++;
   idx2++;

   cout << "idx1.value:" << idx1.GetIndex() << endl;
   cout << "idx2.value:" << idx2.GetIndex() << endl;


 }

The statement idx2++ is giving me a compilation error.The prefix however i.e ++idx1 is working properly.The book I'm referring to says that both should give the same output...i.e the value member must get incremented by 1.

Why am I facing this problem??...The IDE I'm using is visual studio 2015.

2 Answers2

2

Prefix and postfix ++ are two separate operators. C++ differentiates them by taking a dummy int param for postfix and no param for prefix.

lorro
  • 10,687
  • 23
  • 36
1

https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

The signature for the postfix-increment operator overload is TYPE operator ++(int)

tkausl
  • 13,686
  • 2
  • 33
  • 50