我试图覆盖类的==运算符,但是比较似乎以某种方式失败。例如,当我编写与称为eq的函数相同的代码时,不会发生任何问题。
class geo
{
...
bool operator==(geo const& other)
{
if(_id != other._id) return false;
if(!(name == other.name))return false;
if(is_primary!=other.is_primary)return false;
for(int i = 0; i<6;i++){
if(position[i]!=other.position[i])
return false;
}
return true;
}
....
private:
int _id, is_primary;
vector position;
string name;
}
Run Code Online (Sandbox Code Playgroud)
主要功能:...
geo* i= new geo(2.1, 2.82, 1, 0, 0, 180, 0, "Patient-1",1);
geo* j= new geo(2.1, 2.82, 1, 0, 0, 180, 0, "Patient-1",1);
if(i==j)
std::cout<<"they are equal\n";
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此命令时,它表示i和j有所不同。知道我在哪里做错事了吗?
编辑:谢谢盖兹的评论。我刚刚解决了;上面显示的代码可以正常工作。当然,如果我正在尝试简化代码,以使此处粘贴可读性。因此,我正在更新上面的代码以使其成为问题,以便将来的读者可能会看到比我更好的解决方案,同时我可以学到更多。