Tools.h++ Manual

104011 Tandem Computers Incorporated 12-7
12
1. Find an object with a particular state. An example is testing two strings for
the same value. In the literature, this is frequently referred to as two objects
testing "isEqual", having "equality", "compares equal", having the same
"value", or testing true for the "=" operator. Here, we will refer to the two
objects as testing equal ("isEqual"). In general, it is necessary to have some
knowledge of the two object's type (or subtype, in the case of inheritance) in
order to find the appropriate instance variables to test for equality.
1
2. Finding a particular object (that is, one with the same identity). In the
literature, this is referred to as two objects testing "isSame", or having the
same "identity", or testing true for the "==" operator. We will refer to this as
two objects having the same identity. Note that because value-based
collection classes make a copy of an inserted object, finding an object in a
value-based collection class with a particular identity is meaningless.
In C++, to test for identity (that is, whether two objects are, in fact, the same
object) you must test to see if they have the same address.
Note – In C++, because of multiple inheritance, the address of a base class and
its associated derived class may not be the same. Because of this, if you
compare two pointers (i.e., two addresses) to test for identity, the types of the
two pointers should be the same.
Smalltalk uses the operator "=" to test for equality, the operator "==" to test for
identity. However, in the C++ world, operator"=" has become firmly attached
to meaning assignment, operator "==" to generally meaning equality of values.
This is the approach we have taken.
Note – In the Tools.h++ Classes, the operator "==", when applied to two
classes, generally means test for equality of values ("isEqual"). Of course,
when applied to two pointers, it means test for identity.
Whether to test for equality or identity will depend on the context of your
problem. Here are some examples that might help.
1. The Tools.h++ collection classes allow a generalized test of equality. It is up to you to define what it means
for two objects to “be equal.” That is, a bit-by-bit comparison of the two objects is not done. You could define
“equality” to mean that a panda is the same as a deer because, in your context, both are mammals.