Two C + + objects, such as the programmer's own definition, usually the override = = operator

Source: Internet
Author: User

I have been for many years to Java's = = and equals the difference and contact is not clear, later made clear, note here:


To dig out more details, in fact, just understand a principle: two objects in memory is two different areas, even if all its content exactly the same, it is still two different objects. Programming is just a description of logic, in some cases we have to define what is called two objects equal. This is up to you, you can define the first address of two objects equal to the equality of objects (in fact, it is impossible to achieve), you can define their hashcode equality is equal to the object, or only the object name is equal even if the object is equal. This idea is common in oo and is not only unique to Java. For example, this description is found in the QT documentation today:

bool Qvector::contains (const T & value ) const

Returns true if the vector contains an occurrence of value; otherwise returns false.

This function requires the value type to has an implementation of operator== ().

Is that qvector don't understand what you mean by inclusion. So you have to define the = = operator separately for data of type T. I think, compare two objects equal enough trouble, unless the logic really needs to save and manipulate the object directly, otherwise, as long as the comparison of two objects of the pointer (the first address) can be. that would be easier. Just like the Tlist in Delphi, the core management class of RTL, many RTL classes are implemented by it, but tlist is not usually just for holding pointers (but it happens to be All tobject.create returns are pointers on the heap, which can be cumbersome if the object is saved directly.

Two C + + objects, such as the programmer's own definition, usually the override = = operator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.