Tools.h++ Manual

15-10 104011 Tandem Computers Incorporated
15
Program output:
5
Mary
2
Here's the line-by-line description:
1–7. These lines are as in “Example” on page 4.
8. Insert another instance with the value "Mary".
9. This statement prints out the total number of entries in the sorted
collection: 5.
10. A throwaway variable "dummy" is constructed, to be used to test for the
occurrences of strings containing "Mary".
11. The collection is asked to return a pointer to the first object encountered
that compares equal to the argument. A nil pointer (zero) is returned if
there is no such object.
12. The pointer is tested to make sure it is not nil.
13. Paranoid check. In this example, it is obvious that the items in the
collection must be of type
RWCollectableString
. In general, it may
not be obvious.
14. Because of the results of step 13, the cast to a
RWCollectableString
pointer is safe. The pointer is then dereferenced and printed.
if(t){ // 12
if(t-isA() == dummy.isA()) // 13
cout << *(RWCollectableString*)t << "\n"; // 14
}
else
cout << "Object not found.\n"; // 15
cout << sc.occurrencesOf(&dummy) << "\n"; // 16
return 0;
}
Code Example 15-1 (Continued)