Destroying collections

Figure 1 shows the protocol used to destroy collections.


Destroying collections is done in two steps. First the elements of the collection are destroyed, then the collection itself is destroyed. This discussion assumes that both collections and collection elements have been created with the new operator and thus occupy heap memory.

Most of the example programs which manage collections in this chapter use the following two statements to destroy collections.

          collection->DeleteAll();
          delete collection;
The first statement iterates over the elements of the collection and destroys each element in turn. You can achieve the same effect manually with an iterator.

          TIteratorOver<TCollectibleLong>* element =
              collection->CreateIterator();
          for (TCollectibleLong* number = element->First();
               number != NIL;
               number = element->Next(), i++) {
              delete element;
          }
Thus DeleteAll saves considerable work for a frequently used operation. If you have acquired the elements you have inserted into a collection from another resource and you do not own the memory associated with them, use RemoveAll instead of DeleteAll as explained in the previous section.

The previous section "Removing elements" also covers aspects of destroying collections related to individual collection elements.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker