在c++中,定义一个结构体,重载操作符==,不表示默认就可以用!=操作符。下段代码是有问题的
#include <iostream>
using namespace std;
struct Point
{
int x, y;
Point(int _x = 0, int _y = 0): x(_x), y(_y) {}
bool operator == (const Point &other)
{
return x == other.x && y == other.y;
}
};
int main()
{
Point a(1, 2);
Point b(2, 1);
cout << boolalpha << (a != b) << endl;
return 0;
}
需要重载!=操作符