Tools.h++ Manual
104011 Tandem Computers Incorporated 15-11
15
15. If the pointer
t
was nil, then an error message would have been printed
here.
16. The call to
occurrencesOf()
returns the number of items that compare
equal to its argument. In this case, two items are found (the two
occurrences of "Mary").
remove() functions
virtual RWCollectable* remove(const RWCollectable*);
virtual void removeAndDestroy(const RWCollectable*);
The function
remove()
looks for an item that is equal to its argument and
removes it from the collection, returning a pointer to it. It returns nil if no item
was found.
The function
removeAndDestroy()
is similar except that rather than return
the item, it deletes it, using the virtual destructor inherited by all
RWCollectable
items. You must be careful when using this function that the
item was actually allocated off the heap (i.e. not the stack) and that it is not
shared with another collection.
Expanding on the example above:
RWCollectable* oust = sc.remove(&dummy); // 17
delete oust; // 18
sc.removeAndDestroy(&dummy); // 19
17. Removes the first occurrence of the string containing "Mary" and returns
a pointer to it. This pointer will be nil if there was no such item.
18. Delete the item (which was originally allocated off the heap). There is no
need to check the pointer against nil because the language guarantees
that it is always OK to delete a nil pointer.
19. In this statement, the remaining occurrence of "Mary" is not only
removed, but also deleted.