overloading postfix and prefix operators

前端 未结 4 1747
既然无缘
既然无缘 2020-11-30 09:23

please consider following code

#include 
using namespace std;
class Digit
{

private:
    int m_digit;
public:
    Digit(int ndigit=0){
             


        
4条回答
  •  Happy的楠姐
    2020-11-30 10:04

    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

提交回复
热议问题