How to differentiate (when overloading) between pr

2019-01-12 00:11发布

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?

4条回答
迷人小祖宗
2楼-- · 2019-01-12 01:04
Melony?
3楼-- · 2019-01-12 01:08

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
};
查看更多
叼着烟拽天下
4楼-- · 2019-01-12 01:08

Postfix has an int argument in the signature.

Class& operator++();    //Prefix 
Class  operator++(int); //Postfix 
查看更多
我想做一个坏孩纸
5楼-- · 2019-01-12 01: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.

查看更多
登录 后发表回答