简体   繁体   中英

Operator Function Called Infinite

I am getting runtime error, Could anyone figure out why there is an infinite call in this programme and which line is doing it

http://ideone.com/0CWZTD

Here goes my Code

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

will again invoke your operator overload, so you are basically doing:

A(){
   A();
}

This is a self recursive function call, and you are obviously not making progress (towards the base case, even worse...there is no base case)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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