Three functions deal with the same data, appears, Below, An example of compareto() – HP Integrity NonStop J-Series User Manual
Page 222

RWBoolean Bus::isEqual(const RWCollectable* c) const
{ const Bus* b = (const Bus*)c;
return busNumber_ == b->busNumber_;
}
Here we are considering buses to be equal if their bus numbers are the same. Again, other choices
are possible.
Virtual Function hash()
The function hash() should return an appropriate hashing value for the object. Here is the function's
declaration:
unsigned hash() const;
A possible definition of hash() for our class Bus might be:
unsigned Bus::hash() const{
return (unsigned)busNumber_;
}
The example above simply returns the bus number as a hash value. Alternatively, we could choose
the driver's name as a hash value:
unsigned Bus::hash() const{
return driver_.hash();
}
In the above example, driver_ is an
RWCString
that already has a hash function defined.
Note: we expect that two objects that test TRUE for isEqual will hash to the same value.
An Example of compareTo(), isEqual(), and hash()
We've described three inherited virtual functions: compareTo(), isEqual(), and hash(). Here is an
example that defines a set of objects, and applies the functions. The results of the functions appear
as comments in the code.
RWCollectableString a("a");
RWCollectableString b("b");
RWCollectableString a2("a");
a.compareTo(&b); // Returns -1
a.compareTo(&a2); // Returns 0 ("compares equal")
b.compareTo(&a); // Returns 1
a.isEqual(&a2); // Returns TRUE
a.isEqual(&b); // Returns FALSE
a.hash() // Returns 96 (operating system dependent)
Note that the compareTo() function for
RWCollectableString
s has been defined to compare strings