The following code has 2 definitions of operator+
- one is on the class Foo
, and the other one is a standalone function.
I feel like the compiler should have complained about this, but it didn't. When I use operator+
in the main function, it picks the one defined in the class. When I delete the one in the class, it starts using the standalone function.
The fact that deleting a class method silently changes the behavior of the C++ program is very concerning. Is there a rationale behind this?
#include <iostream>
class Foo
{
public:
int operator+(const Foo& b)
{
return 5;
}
};
int operator+(const Foo& a, const Foo& b)
{
return 6;
}
int main()
{
Foo a, b;
int c{ a + b };
std::wcout << c << std::endl;
return 0;
}