Smalltalk-like Classes Example
To orient ourselves, we always like to start with an example. The following example uses a SortedCollection to store and order a set of RWCollectableStrings. SortedCollection is actually a typedef for the Smalltalk-like collection class RWBinaryTree. Objects inserted into it are stored in order according to their relative values as returned by the virtual function compareTo(). (See Add Definitions for Virtual Functions.)
Here is the code:
 
#define RW_STD_TYPEDEFS 1 //1
#include <rw/bintree.h>
#include <rw/collstr.h> //2
#include <rw/rstream.h>
using std::cout;
using std::endl;
int main(){
// Construct an empty SortedCollection
SortedCollection sc; //3
// Insert some RWCollectableStrings:
sc.insert(new RWCollectableString("George")); //4
sc.insert(new RWCollectableString("Mary"));
sc.insert(new RWCollectableString("Bill"));
sc.insert(new RWCollectableString("Throkmorton"));
// Now iterate through the collection printing all members:
RWCollectableString* str; //5
SortedCollectionIterator sci(sc); //6
while( str = (RWCollectableString*)sci() ) //7
cout << *str << endl; //8
sc.clearAndDestroy();
return 0;
}
Program Output:
 
Bill
George
Mary
Throkmorton
The following explains the code line-by-line:
//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 Creates an empty SortedCollection.
//4 This, and the following lines, create four RWCollectableStrings off the heap and inserted into the collection, in no particular order. See the SourcePro API Reference Guide for details on constructors for class RWCollectableString. 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.
//5 Declares and defines a pointer to an RWCollectableString.
//6 Constructs an iterator from the SortedCollection sc.
//7 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 Essential Tools Module iterators work this way. See Stroustrup (1986, Section 7.3.2) for an example and discussion of iterators, as well as Iterators for 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.
//8 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.