简体   繁体   中英

How to differentiate (when overloading) between prefix and postfix forms of operator++? (C++)

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?

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
};

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 .

Postfix has an int argument in the signature.

Class& operator++();    //Prefix 
Class  operator++(int); //Postfix 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM