Tools.h++ Manual

104011 Tandem Computers Incorporated 17-9
17
Note – there are many other possible choices—we could have used the driver
name, in which case they would have been sorted by that. Which choice you
use will depend on your particular problem.
Of course, there is a hazard here. We have been glib in assuming that the
actual type of the
RWCollectable
which
c
points to is always a
Bus
. If a
careless user inserted, say, a
RWCollectableString
into the collection, then
the results of the cast (
const Bus*
)
c
would be invalid and dereferencing it
could bring disaster. This is a glaring deficiency in C++ that the user must
constantly be aware of. The necessity for all virtual functions to have all the
same signatures requires that they return the lowest common denominator, in
this case, class
RWCollectable
. The result is that all compile-time type
checking breaks down.
One must be careful that the members of a collection are either homogeneous
(i.e., all of the same type), or that there is some way of telling them apart. The
member function
isA()
can be used for this.
17.8 Virtual function isEqual()
RWBoolean isEqual(const RWCollectable* c) const;
The virtual function
isEqual()
plays a similar role to the “tester function” of
the generic collection classes (see “Tester functions” on page 4 in Chapter 14,
““Generic” Collection Classes”).
Note – The function "
RWBoolean isEqual(const RWCollectable*)
"
should return
TRUE
if the object and its argument are considered “equal”,
FALSE
otherwise. The definition of “equal” 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.
isEqual
” need not necessarily be defined as “being identical”, that is, having
the same address (although this is the default), but rather, that they 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 inherit
type
RWCollectable
. However, you are responsible for making sure that any
typecasts you do are appropriate.