2015-10-17 22 views
-2

我得到运行时错误,谁能弄清楚为什么会出现在这个节目的无限通话和线路做操作函数调用无限

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

你认为'* this == temp'会打电话吗? – Chad

+1

安装一个调试器就可以解决这个问题 – deW1

+0

Ya Right deW1实际上我的大脑因为连续学习15个小时而感到疲倦,所以无法想出 –

回答

0

因为*this == temp相当于(*this).operator==(temp),显然这与你刚才写的operator==相同。

0
*this == temp 

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

A(){ 
    A(); 
} 

这是一个自递归函数调用,你显然不能取得进展(朝向基本情况,更有甚者...没有基本情况)