15

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 instead of checking if they are exactly same objects?

10
  • 4
    If the two objects have the same address, they are the same. Though if two objects have the same attributes, they are not necessarily the same...
    – SinisterMJ
    Commented Sep 13, 2012 at 9:54
  • 7
    Well, do you want to check that the objects are equal, or whether they are the same object? This is not the same. Commented Sep 13, 2012 at 9:54
  • Ah I guess I worded my title wrong, I'm seeking if they are the same object and not different objects with equal value; Commented Sep 13, 2012 at 9:57
  • Who are "they"? Two pointers? Two references? A pointer and a value? Two values obviously cannot be "the same object"
    – Kos
    Commented Sep 13, 2012 at 9:58
  • 1
    Personally my wording would be that two objects "are the same object" if and only if they have the same address, and "have the same value" or "are equal" if the comparison of attributes returns true. To be really pedantic, I'd have to say "two complete objects", because it's possible for two "different" subobjects to have the same address if the implementation uses the empty base class optimization. And obviously union members can have the same address. Commented Sep 13, 2012 at 10:01

4 Answers 4

38

EDIT: Beware: you cannot pass values (objects) to your function if you want it to work correctly. You need to pass either (probably const) references or pointers.

If you just want to know whether both references or pointers point to the same object (not identical objects, but the same), comparing the addresses is the right thing to do, indeed:

bool AreEqual(const Class& a, const Class& b)
{
  return &a == &b;
}

Note that the & operator may be overloaded for the Class class above. Since C++11 the function template std::addressof is available to cope with that fact:

#include <memory> //std::addressof
bool AreEqual(const Class& a, const Class& b)
{
  return std::addressof(a) == std::addressof(b);
}
4
  • @LuchianGrigore the wording took a wrong turn near "both [objects]" if we want to be strict :)
    – Kos
    Commented Sep 13, 2012 at 9:57
  • @Kos: If anybody knows how to express that correctly in English, I will gladly update my answer :)
    – Gorpik
    Commented Sep 13, 2012 at 10:00
  • 1
    @Gorpik how about "If you want to know whether two given pointers or references point to the same object"?
    – codeling
    Commented Sep 13, 2012 at 10:02
  • 1
    @nyarlathotep: I had not considered that because I had read objects in the question, but this leads me into something else. I am updating the answer.
    – Gorpik
    Commented Sep 13, 2012 at 10:50
17

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.

4
  • 2
    Though identity entails equivalence, is often quicker to test (often very much so) and comparing something with itself comes up often enough, that when you do want to support equivalence it can be worth testing for identity as a short-cut, too.
    – Jon Hanna
    Commented Sep 13, 2012 at 10:33
  • 2
    However let's not confuse concepts with optimizations Commented Sep 13, 2012 at 10:40
  • I think it's worth it in this case, because in itself that it works shows the relationship between the two concepts: An object must always be equivalent to itself, if it isn't, you are doing it wrong.
    – Jon Hanna
    Commented Sep 13, 2012 at 10:52
  • Actually, while reference comparison makes sense in languages as Java or C# where instances of user defined classes are always heap allocated, I don't find it as reasonable for C++ value classes, which are usually allocated directly. Commented Sep 13, 2012 at 15:11
3

I suppose that you make a proper distinction between same and equal.

Two pointers pointing to the same address means that they point to the same object. So yes: same address means same object and therefore equal (although equality makes sense only if we talk about more than 1 object).

Same attributes don't necessarily mean the same object. E.g. you can have two users with the same name "John Doe". The objects representing them would still be different objects, so they can't be used interchangeably. However, if you have a Point class, then two different instances of {1, 2} really represent the same thing and can be used interchangeably.

There is a larger issue of difference between value objects and reference objects or entities, so I suggest to look it up.

E.g. if you have a Point class, then two different instances of {1, 2} really represent the same thing, unlike the User example before.

2

If you have objects for which comparison is time consuming, comparing the pointers could be used as a fast way to determine object equivalence (i.e., if the pointers are equal, objects are equivalent, otherwise they may still be equivalent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.