使用bool运算符==比较对象 [英] Comparing objects using bool operator==

查看:147
本文介绍了使用bool运算符==比较对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,在阅读一些SO问题和答案后,我仍然不明白为什么使用

So, after reading some SO questions and answers, i still doesn't understand why use

friend bool operator==( BaseClass const &left, BaseClass const &right )

而不是

bool operator==( BaseClass const &right )

现在我有类似的东西 http://pastebin.com/pKsTabC0 (固定) - 它似乎工作正常。但也许我错过了什么?任何建议?

right now I have something like this http://pastebin.com/pKsTabC0 (Fixed) - and it seems to work fine. But maybe I'm missing something? Any Suggestions?

更新1

工作权 http://ideone.com/fIAmB 。删除不必要的虚拟和添加const。仍然我不明白为什么要使用朋友...

Ok i changed the source to make it work right http://ideone.com/fIAmB. Removed unnecessary virtual and added const. Still i dont understand why to use friends...

推荐答案

您的衍生函数不具有与父运算符,因此隐藏父项比较而不是覆盖它。这意味着你不能使用虚拟形式,因为左手参数的静态类型决定了被调用的函数。

Your derived function doesn't have the same signature as the parent operator, so it hides the parent comparison rather than overriding it. This means that you can't make use of the virtual-ness anyway since the static type of the left hand argument determines the function that's called.

这就是为什么正常的方法对于虚拟比较是一个非成员相等运算符,分派到基类中的虚拟比较函数。

That's why the normal method for virtual comparison is a non-member equality operator that dispatches to a virtual comparison function in the base class.

请考虑你对虚拟比较的特定需求,

Do consider your specific need for virtual comparison though as it may be a design smell that there's an alternate design you could use instead.

还要注意,成员比较(在这种情况下是相等的)运算符通常应该是 const

Also note that member comparison (equality in this case) operators should usually be const.

编辑:你可能只关心基于左手参数的静态类型的比较,这应该更容易问题。在这种情况下,你的代码处理所有情况,除了左边的参数是隐式转换为 Base Derived 通过一些机制而不是继承(转换运算符或转换构造函数)。如果你不在乎这些情况,那么成员的平等是好的。

It seems that you may only care about comparison based on the static type of the left hand argument, which should be an easier problem. In this case your code handles all cases except where the left hand argument is a type implicitly converts to a Base or Derived by some mechanism other than inheritance (conversion operator or converting constructor). If you don't care about those cases, then the member equality is fine.

最后要说明的是,如果比较可以完全通过公共接口完成, (几乎)总是喜欢非成员,非朋友的函数,而不管它是否是运算符。

As one final note, if the comparison could be done entirely through the public interface, we (almost) always prefer non-member, non-friend functions regardless of whether or not it's an operator.

EDIT2(非常快速地概述非成员,朋友):

EDIT2 (A really quick overview of non-member, non-friend):

例如假设你的类有一个public key 方法,如果两个实例的键等于你想调用的对象相等。然后,不使用朋友或成员相等运算符,你可以写独立的平等:

For example suppose your class has a public key method, and if the keys of two instances are equal you want to call the objects equal. Then, without using friends or a member equality operator, you can write your equality standalone:

bool operator==(const MyType& left, const MyType& right)
{
    return left.key() == right.key();
}

这篇关于使用bool运算符==比较对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆