Virtual Function isEqual()
The virtual function isEqual() is a tester function whose purpose is to signal when a certain member of the collection has been identified.
bool isEqual(const RWCollectable* c) const;
The function
bool isEqual(const RWCollectable*) should return
true if the object and its argument are considered equal, and
false otherwise. The definition of equality is left to the class designer. The default definition, as defined in class
RWCollectable, is to test the two addresses for equality, that is, to test for
identity.
Note that
isEqual() does not have to be defined as being identical. Rather
isEqual can mean that two objects are equivalent in some sense. In fact, the two objects need not even be of the same type. The only requirement is that the object passed as an argument must inherit type
RWCollectable. You are responsible for making sure that any typecasts you do are appropriate.
Also note that there is no formal requirement that two objects that 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. It is also possible to design a class for which the isEqual test returns true for objects that have different hash values. This would make it impossible to search for such objects in a hash-based collection.
For the Bus class, an appropriate definition of isEqual might be:
bool 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.