C++ 比较运算符重载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10575766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:14:09  来源:igfitidea点击:

Comparison operator overloading

c++operators

提问by blaze

Which is best practice (in this case):

这是最佳实践(在这种情况下):

bool Foo::operator==(const Foo& other) {
  return bar == other.bar;
}

// Implementation 1
bool Foo::operator!=(const Foo& other) {
  return bar != other.bar
}

// Implementation 2
bool Foo::operator!=(const Foo& other) {
  return !(*this == other);
}

For operators like >, <, <=, >= I would go with implementation 2 when possible. However, for != I think implementation 1 is better since another method call is not made, is this correct?

对于像 >、<、<=、>= 这样的运算符,我会尽可能使用实现 2。但是,对于 != 我认为实现 1 更好,因为没有进行另一个方法调用,这是正确的吗?

采纳答案by zneak

The second implementation has the notable constraint that ==will always be the boolean opposite of !=. This is probably what you want, and it makes your code easier to maintain because you only have to change one implementation to keep the two in sync.

第二个实现有一个显着的约束,它==总是与 的布尔相反!=。这可能就是您想要的,它使您的代码更易于维护,因为您只需更改一个实现即可使两者保持同步。

回答by chris

You should always use what you have when overloading comparison operators. The only two you should need to define are operator==and operator<. The rest you can write in terms of these two. It's less error-prone, as if you have a mistake, it's only in one place.

在重载比较运算符时,您应该始终使用现有的内容。唯一需要定义的两个是operator==operator<。其余的你可以根据这两个来写。它不太容易出错,就好像你有错误一样,它只在一个地方。

One of the main features of OOP is code reusability. If you've already written the code, why write it again? Stick to what you have, and you'll only have one thing to test.

OOP 的主要特性之一是代码可重用性。如果你已经写了代码,为什么还要写一遍?坚持你所拥有的,你将只需要测试一件事。

It's rather like declaring a constant, and then using it in several spots throughout your file.

这更像是声明一个常量,然后在整个文件的多个位置使用它。

回答by sashang

Implementation 2 is better because it makes use of the already defined operator==. Also those operator functions should be constbecause they don't modify the object.

实现 2 更好,因为它使用了已经定义的 operator==。这些运算符函数也应该是const因为它们不修改对象。

回答by Edward Strange

None of the above.

以上都不是。

I wish I could find the paper that really goes over this in detail but I can't recall the name.

我希望我能找到真正详细讨论这个问题的论文,但我不记得名字了。

Your comparison operations should be external. Your interface should be sufficient to finding the state of an object and the state of the object should be dictating comparison. It should be possible to write "equals" outside of your class, and thus any comparison really, and that being possible...you want to.

您的比较操作应该是外部的。您的界面应该足以找到对象的状态,并且对象的状态应该决定比较。应该可以在你的班级之外写出“等于”,因此任何比较真的,这是可能的......你想。

回答by Shahbaz

In general, implementation 2 is better for many reasons. First of all, you don't write (almost) duplicate code. If you need to change it (because the class has grown or there has been a bug), again with implementation 2 you change only 1 place. That is, implementation 2 makes your code more consistent and less error prone.

一般来说,出于多种原因,实现 2 更好。首先,您不会编写(几乎)重复的代码。如果您需要更改它(因为类已经增长或存在错误),再次使用实现 2,您只更改 1 个位置。也就是说,实现 2 使您的代码更加一致且不易出错。