Tools.h++ Manual

17-10 104011 Tandem Computers Incorporated
17
There is no formal requirement that two objects which “compare equal” (i.e.,
compareTo()
returns zero) must also return
TRUE
from
isEqual()
, although
it is hard to imagine a situation where this wouldn’t be the case.
For our example above, an appropriate definition might be:
RWBoolean Bus::isEqual(const RWCollectable* c) const
{
const Bus* b = (const Bus*)c;
return busNumber_ == b->busNumber_;
}
Here we are considering busses to be “equal” if their bus numbers are the
same. Again, other choices are possible.
17.9 Virtual function hash()
unsigned hash() const;
Note – The function
hash()
should return an appropriate hashing value for
the object.
A possible definition for
hash()
for our example might be:
unsigned Bus::hash() const
{
return (unsigned)busNumber_;
}
Here, we have just returned the bus number as a hash value. Alternatively, we
could have hashed the driver’s name:
unsigned Bus::hash() const
{
return driver_.hash();
}