|
|
|
|
The CList class provides the means to create a doubly linked list of items. Traversing the list is by means of a POSITION object. When should a CList be used? The CList is most appropriate for collections in which quick random access is not important and items will be inserted or deleted from the beginning or middle of the list fairly often. It is easy to remove the first or last item from the list. It is not easy to access the nth item in the list. DeclaringThe declaration of a CList takes a little getting used to but is actually very simple. The first argument is the type of the value that is actually being stored. The second argument defines what the type is of the value that is going in or coming out of the class. As an example, let’s define a CList that can contain a list of CStuff objects. First, we have to have the CStuff class. class CStuff Now we can declare the list. CList< CStuff*, CStuff* > m_lstStuff; DestructionIn the previous example, notice that we declared the list to contain pointers to CStuff objects. When the class destructor is called, only the pointers will be released. Whatever the pointers reference will not automatically be released. The programmer must take this sort of thing into account when using CList and be sure to release the actual objects themselves. IteratingAssume that we have a CList of classes defined as CList< CStuff*, CStuff* > m_lstStuff; To iterate through the list, try this: POSITION pos; |
|
|