称为Infinite的运算符函数

Operator Function Called Infinite

本文关键字:函数 运算符 Infinite 称为      更新时间:2023-10-16

我得到运行时错误,谁能弄清楚为什么在这个程序中有一个无限调用,哪一行正在做它

http://ideone.com/0CWZTD

这是我的代码

class opOverload{
public:
    bool operator==(opOverload temp){
        if(*this == temp){
            cout << "both same";
            return true;
        }
        else{
            cout <<"both different";
            return false;
        }
    }
};

int main() {
    // your code goes here
    opOverload a1,a2;
    a1==a2;
    return 0;
}

因为*this == temp等于(*this).operator==(temp),它显然调用了您刚才写的相同的operator==

*this == temp

将再次调用你的操作符重载,所以你基本上是在做:

A(){
   A();
}

这是一个自我递归函数调用,你显然没有取得进展(向基本情况,甚至更糟…