beautypg.com

HP Integrity NonStop J-Series User Manual

Page 107

background image

In C++, to test for identity_that is, to test whether two objects are the same object_you must see
if they have the same address. Because of multiple inheritance, the address of a base class and
its associated derived class may not be the same. Therefore, if you compare two pointers
(addresses) to test for identity, the types of the two pointers should be the same.

Smalltalk uses the operator = to test for equality, and the operator == to test for identity. In the
C++ world, however, operator = is firmly attached to assignment, and operator == to some kind
of equality of values. We have taken the C++ approach. At Rogue Wave, the operator ==
generally means test for equality of values (isEqual) when applied to two classes, and test for
identity when applied to two pointers.

Whether to test for equality or identity depends on the context of your problem. Here are some
examples that can clarify which to choose.

Here's an example when you should test for equality. Suppose you are maintaining a mailing
list. Given a person's name, you want to find his or her address. In this case, you search for a
name that is equal to the name at hand. An

RWHashDictionary

would be appropriate. The key

to the dictionary would be the name, the value would be the address.

In the next example, you would test for identity. Suppose you are writing a hypertext
application, and need to know in which document a particular graphic occurs. You could keep
an

RWHashDictionary

of graphics and their corresponding documents. In this case, however,

you need an

RWIdentityDictionary

because you need to know in which document a particular

graphic occurs. The graphic acts as the key, the document as the value.

Maintaining a disk cache? You might want to know whether a particular object is resident in
memory. In this case, an

RWIdentitySet

is appropriate. Given an object, you can check to see

whether it exists in memory -- another identity test.

This manual is related to the following products: