Tools.h++ Manual

104011 Tandem Computers Incorporated 15-5
15
1. By defining the preprocessor macro
RW_STD_TYPEDEFS
we enable the
set of Smalltalk-like typedefs. This enables us to use the generic name
"
SortedCollection
" instead of
RWBinaryTree
, its true identity.
2. The second
#include
declares class
RWCollectableString
which is a
derived class with classes
RWCString
and
RWCollectable
as base
classes. Multiple inheritance was used to create this class. Most of its
functionality is inherited from class
RWCString
. Its ability to be
"collected" was inherited from class
RWCollectable
.
3. An empty
SortedCollection
was created at this line.
4–7. Four
RWCollectableStrings
were created off the heap and inserted
into the collection. See Part 2: Class Reference for details on constructors
for
RWCollectableStrings
. The insertions were not done in any
particular order.
8. A pointer to a
RWCollectableString
was declared and defined here.
9. An iterator was constructed from the
SortedCollection sc
.
10. The iterator is then used to step through the entire collection, retrieving
each value in order. The function call operator (i.e.,
operator()
) has
been overloaded for the iterator to mean "step to the next item and return
a pointer to it". All Tools.h++ iterators work this way. See Stroustrup
(1986, Section 7.3.2) for an example and discussion of iterators, as well as
“Iterators” on page 8 in Chapter 12, “Introduction to Collection Classes,”
of this manual. The typecast
str = (RWCollectableString*)sci()
is necessary because the iterator returns a
RWCollectable*
(that is, a
pointer to a
RWCollectable
) which must then be cast into its actual
identity.
11. Finally, the pointer
str
is dereferenced and printed. The ability of a
RWCollectableString
to be printed is inherited from its base class
RWCString
.
When run, the program prints out the four collected strings "in order". For
class
RWCollectableString
, this means in lexicographical order.