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?
-
4If 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...– SinisterMJCommented Sep 13, 2012 at 9:54
-
7Well, do you want to check that the objects are equal, or whether they are the same object? This is not the same.– juanchopanzaCommented 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;– user1527216Commented 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"– KosCommented Sep 13, 2012 at 9:58
-
1Personally 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.– Steve JessopCommented Sep 13, 2012 at 10:01
4 Answers
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);
}
-
@LuchianGrigore the wording took a wrong turn near "both [objects]" if we want to be strict :)– KosCommented Sep 13, 2012 at 9:57
-
@Kos: If anybody knows how to express that correctly in English, I will gladly update my answer :)– GorpikCommented 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"?– codelingCommented 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.– GorpikCommented Sep 13, 2012 at 10:50
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.
-
2Though 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. Commented Sep 13, 2012 at 10:33
-
2However 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. 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
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.
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)