8

Because I've overloaded the operator++ for an iterator class

template<typename T>
typename list<T>::iterator& list<T>::iterator::operator++()
{
    //stuff
}

But when I try to do

list<int>::iterator IT;
IT++;

I get a warning about there being no postifx ++, using prefix form. How can I specifically overload the prefix/postifx forms?

Jens Erat
  • 37,523
  • 16
  • 80
  • 96

4 Answers4

20

http://www.devx.com/tips/Tip/12515

class Date {
    //...
    public:
    Date& operator++(); //prefix
    Date& operator--(); //prefix
    Date operator++(int unused); //postfix
    Date operator--(int unused); //postfix
};
zaratustra
  • 8,148
  • 8
  • 36
  • 42
  • 5
    Postfix operators should return by value, not reference. I guess there might be very strange situations where they can return a reference, but what to? Not this, because it has been incremented... – Steve Jessop May 21 '09 at 20:15
12

Write a version of the same operator overload, but give it a parameter of type int. You don't have to do anything with that parameter's value.

If you're interested in some history of how this syntax was arrived out, there's a snippet of it here.

Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
8

Postfix has an int argument in the signature.

Class& operator++();    //Prefix 
Class  operator++(int); //Postfix 
e.James
  • 116,942
  • 41
  • 177
  • 214
RC.
  • 27,409
  • 9
  • 73
  • 93
-1

everything about operator overloading http://www.parashift.com/c++-faq-lite/operator-overloading.html

CHANDRAHAS
  • 637
  • 3
  • 10
  • 19