简体   繁体   English

如何区分(当重载时)operator ++的前缀和后缀形式? (C ++)

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

Because I've overloaded the operator++ for an iterator class 因为我已经为迭代器类重载了operator++

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. 我收到有关没有使用前缀形式的postifx ++的警告。 How can I specifically overload the prefix/postifx forms? 如何专门重载prefix / postifx形式?

http://www.devx.com/tips/Tip/12515 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 . 编写相同运算符重载的版本,但给它一个类型为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. Postfix在签名中具有int参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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