beautypg.com

HP Integrity NonStop J-Series User Manual

Page 155

background image

Throkmorton

Let's go through the code line-by-line and explain what is happening:

//1

By defining the preprocessor macro RW_STD_TYPEDEFS, we enable the set of
Smalltalk-like typedefs. We can then use the typedef SortedCollection instead of

RWBinaryTree

, its true identity.

//2

The second #include declares class

RWCollectableString

, a derived class that multiply

inherits from its base classes

RWCString

and

RWCollectable

. RWCollectableString

inherits functionality from RWCString, and "ability to be collected" from class
RWCollectable.

//3

An empty SortedCollection was created at this line.

//4 - //7 Four

RWCollectableString

s were created off the heap and inserted into the collection,

in no particular order. See the Class Reference for details on constructors for
RWCollectableStrings. The objects allocated here normally should be deleted before
the end of the program, but we omitted this step to make the example more concise.

//8

A pointer to an

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 operator()has been overloaded so that the iterator
means "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

Chapter 10 (

Iterators in Collection Classes

)

of this manual. The

typecast:

str = (RWCollectableString*)sci()

is necessary because the iterator returns an RWCollectable*; that is, a pointer to an

RWCollectable

which must then be cast into its actual identity.

//11

Finally, the pointer str is dereferenced and printed. The ability of an

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 lexicographical order.

This manual is related to the following products: