Java的equals()和C ++的operator ==有什么区别? [英] What is the difference between Java's equals() and C++'s operator ==?

查看:214
本文介绍了Java的equals()和C ++的operator ==有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关使用 typeid的问题 是C ++,我建议它可以用来比较对象中的类型比较。我没有看到它做得太多,但我记得Java的等于



再多看一点,情况似乎如此:有人说应该比较两个对象的实际类,并且有人说 instanceof 是正确使用的工具,可能是双重调度。当然,有两种情况中的一种最终更合适,但至少两个选项都被视为



在C ++,OTOH中,我几乎找不到比较实际类型的代码。在大多数情况下,使用双重调度(使用 dynamic_cast ),我找不到任何坚持快速类型比较的人在平等开始时做正确的事情检查。



我想知道为什么多态类型比较问题在Java中有两个可接受的解决方案,而在C ++中只有一个似乎被认为是最佳实践?是否存在重大技术差异,或者只是采用不同的方法?



注意:我的主张是基于印象而非具体知识。如果它们是错的并且Java和C ++在这方面确实相似 - 或者由于上述原因以外的原因而不同,那么它显然是一个可接受的答案。

解决方案

在Java中,所有类型最终都来自 Object ,以及 Object
定义了一个虚函数 Object.equals(Object other),所以你
可以将任何东西与其他东西进行比较,无论它是否为
感觉与否。在C ++中,没有univeral base,
没有 == 的隐式定义。 == 通常只有
被覆盖才有意义,因为比较
相同类型的对象,如果你写无意义的话编译器就会抱怨
代码。在存在继承层次结构的情况下,当然是
,由作者决定 == 是否有意义
(我通常没有,但有很多例外),如果
那么,它对于比较
不同类型的对象应该是什么意思。在层次结构内或其外部:
可能有意义支持 == BigInteger 之间
BigFloat ,例如,即使这些类与
继承无关。



<你没有看到C ++中讨论的问题的原因是
当然,因​​为你没有定义 == 除非有一些
逻辑意义,然后根据
逻辑含义定义它。在Java中,你通常必须定义等于
,所以你必须发明一些含义,然后你得到
讨论发明的内容意思应该是。


In a question regarding the use of typeid is C++, I suggested it could be used to compare types in objects comparison. I haven't seen it done much, but I had Java's equals in mind.

Looking into Java a bit more, this seems to be the case: Some say the actual classes of the two objects should be compared, and some say instanceof is the right tool to use, possibly with double dispatch. There are of course cases in which one of the two is definitively more suitable, but at least both options are considered.

In C++, OTOH, I could barely find code in which the actual types are compared. On most cases, double dispatch is used (with dynamic_cast), and I couldn't find anyone insisting a quick type comparison is the right thing to do at the beginning of the equality check.

I'm wondering why the problem of polymorphic type comparison has two acceptable solutions in Java, while in C++ only one seems to be considered the best practice? Are there significant technical differences, or just different approaches?

Note: My claims are based on impression, not concrete knowledge. If they are wrong and Java and C++ are indeed similar in that aspect - or different for reasons other than the above, it will obviously be an acceptable answer.

解决方案

In Java, all types ultimately derive from Object, and Object defines a virtual function Object.equals(Object other), so you can compare anything with anything else, regardless of whether it makes sense or not. In C++, there is no univeral base, and there is no implicit definition of ==. == is normally only overridden when it makes sense, for comparing objects of the same type, and the compiler will complain if you write nonsense code. In cases where there is an inheritance hierarchy, it is, of course, up to the author to decide whether == makes sense (I usually doesn't, but there are a lot of exceptions), and if so, what it should mean with respect to comparing objects of different types. Within the hierarchy, or outside of it: it might make sense to support == between BigInteger and BigFloat, for example, even if the classes aren't related by inheritance.

The reason you don't see the problem discussed much in C++ is, of course, because you don't define == unless there's some logical meaning for it, and then you define it according to the logical meaning. In Java, you generally have to define equals regardless, so you have to "invent" some meaning, and you get discussion over what the invented meaning should be.

这篇关于Java的equals()和C ++的operator ==有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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