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

♀尐吖头ヾ 提交于 2019-12-17 12:35: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?


回答1:


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.




回答2:


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



回答3:


Postfix has an int argument in the signature.

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



回答4:


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



来源:https://stackoverflow.com/questions/894804/how-to-differentiate-when-overloading-between-prefix-and-postfix-forms-of-oper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!