Equality

Equality between two objects means that the logical contents of the objects are identical in every respect; that is, the two objects can be freely substituted for each other in any context that deals with their values--that excludes address references--without changing the results. As far as the public interfaces are concerned, the two objects always return the same values. The objects can have different internal states that aren't captured in an equality comparison (such as caches or seeds values), but those aren't relevant to the public values of the objects.

Implications

If your comparison compares only one aspect of two objects, name the method accordingly--do not make it an equality operator. (TComparators aren't subject to this restriction, because they explicitly compare only some aspect of the objects.) For example, if your equality operator only ensures that two objects have the same area, you should name it HasSameArea(), not ==. If it only ensures that two objects have the same name, it should be HasSameName().

Ensure that your Hash method is coordinated with your equality. The invariant is: if X==Y, then Hash(X) = Hash(Y). For information on Hash methods, see "Portable hash" on page 113.

Watch for subclasses--in the majority of cases two objects of different classes are not equal. Because of polymorphism, you must check the types of classes to get this right. Because runtime type information (RTTI) isn't currently supported, you will generally do this with an MCollectible check (see the "Equality sample" in the next section).

The equality semantics of surrogate classes should depend on whether they act like pointers or act like values. If they act like pointers, they should compare equal if they refer to the same object. If they act like values, they should compare equal if the objects they refer to compare equal.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker