Can you compare objects by address for equality?

前端 未结 4 2032
旧时难觅i
旧时难觅i 2021-02-13 13:58

I have a function that compares objects by each attribute to see if they are identical. But I was just wondering, would it be better to compare the object by their address inste

4条回答
  •  清歌不尽
    2021-02-13 14:43

    You should decide whether your classes are meant to support equivalence or identity. Equivalence is a property typical of values, such as numbers. Identity is a property typical of entities, such as people.

    Equivalence is usually determined by comparing the data members of a class; comparing addresses is a reasonable way to check for identity.

提交回复
热议问题