overloading postfix and prefix operators

这一生的挚爱 提交于 2019-11-27 18:35:00

问题


please consider following code

#include <iostream>
using namespace std;
class Digit
{

private:
    int m_digit;
public:
    Digit(int ndigit=0){
     m_digit=ndigit;
                        }
    Digit& operator++();//prefix
    Digit& operator--();   //prefix
        Digit operator++(int);
        Digit operator--(int);
        int get() const { return m_digit;}
};
Digit& Digit::operator++(){

   ++m_digit;
   return *this;
}
Digit& Digit::operator--(){
 --m_digit;
 return *this;

}
Digit Digit::operator++(int){
Digit cresult(m_digit);
++(*this);
return cresult;


}
    Digit Digit::operator--(int){
Digit cresult(m_digit);
--(*this);
return cresult;


}
    int main(){

     Digit cDigit(5);
      ++cDigit;
        cDigit++;
         cout<<cDigit.get()<<endl;
         cout<<cDigit.get()<<endl;





     return 0;
    }

here is implemented two version of postfix and prefix operators,i have read that difference is made by introduce another so called dummy argument,but i have question if we see declaration of these

Digit& operator++();//prefix
             Digit& operator--();   //prefix
        Digit operator++(int);
        Digit operator--(int);

they are differed by & mark,so why it is necessary dummy argument?and also in both case for example ++ operator is written before the argument and does not it means that they are same?


回答1:


The pre- and post-increment are two distinct operators, and require separate overloads.

C++ doesn't allow overloading solely on return type, so having different return types as in your example wouldn't be sufficient to disambiguate the two methods.

The dummy argument is the mechanism that the designer of C++ chose for the disambiguation.




回答2:


The operator, just as any function, is identified by the signature. The return type and modifiers before the function/operator name is not included in this. Your operators names are here

operator++()
operator++(int)

This is the only way the compiler distinguishes between the two. As for the Digit& and Digit return values; They are needed because of the way ++x, and x++ are supposed to operate.




回答3:


In C++ functions/methods can't be overloaded by return type, only by parameter list. Ignoring the fact that the prefix and postfix operators are operators, imagine if they were just simple other functions, how would the compiler work out which to use based on the return type? E.g.

int x = 2;

const int DoIt()
{
    return 1;
}

int& DoIt()
{
    return x;
}

int y = DoIt();

Since operator overloads are really just functions at heart, there's no way for the compiler to differentiate between them by return type.

See http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.14




回答4:


In Pre-Increment/Decrement and Post-Increment/decrement, the difference is based on only dummy parameter in overloaded function

operator++()         => Prefix Increment
operator--()         => Prefix Decrement

operator++(int)      => Postfix Increment
operator--(int)      => Postfix Decrement

return type may be same. you can also refer: http://www.tutorialspoint.com/cplusplus/increment_decrement_operators_overloading.htm



来源:https://stackoverflow.com/questions/7740350/overloading-postfix-and-prefix-operators

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