In general you don't need to worry about MCollectible. Many CommonPoint classes have MCollectible as an ancestor. In fact, collections themselves are descendants of MCollectible. This makes it possible for collections to contain other collections (subcollections) in a tree structure. Such trees are used in the CommonPoint application system to implement components of the file system (collections of directories and files) and the view system used to present documents to users. Properties and attributes also use collections.
To focus on basic collection protocol, sample programs use simple classes descending from MCollectible. It is important to understand the relationship between collections and MCollectible objects. You can only create an instance of a collection if you know the type of elements you plan to store in the collection, in this case a subclass of MCollectible. The most simple class descending from MCollectible is TCollectibleLong as shown in Figure 1.
TCollectibleLongs are containers for long integers. You can create an instance of TCollectibleLong with a value of 3 using the following constructor.
TCollectibleLong(3)
Creating a
collection element
The variable pTCollectibleLong can be added directly to any collection declared to contain TCollectibleLongs. You'll notice that a pointer is declared here. Collections expect pointers to elements, rather than actual instances, when elements are added to a collection. Next you'll need to create an collection to hold the TCollectibleLong pointers. The TArrayOf class template is used here to create this collection. Figure 1 shows the inheritance hierarchy for TArrayOf. TCollectibleLong *pTCollectibleLong = new TCollectibleLong(3);
This inheritance diagram indicates that arrays are sequenced collections that can be accessed by numeric keys.
Consider the problem of creating an instance of TArrayOf to contain instances of TCollectibleLong. TArrayOf is a class template that can be used to declare such an array. The constructor used to create an array of TCollectibleLong must be given two arguments. Both can be NIL.
Collection constructor
TArrayOf<TCollectibleLong> is a parameterized class. The above expression invokes a constructor requiring two arguments: a comparator, and a streamer. A NIL for these arguments indicates that the collection has neither a comparator nor a streamer. For now you can ignore the purpose of these arguments, but be aware that it is an error to use the default constructor for descendants of TCollectionOf (that is a constructor expecting no arguments). TArrayOf<TCollectibleLong>(NIL, NIL)
TArrayOf<TCollectibleLong>() // error: don't use default constructor
The compiler won't generate an error if you do this, but that's because the default constructor is made public as a service to other mechanisms (such as streamers) which must use it. You should, however, not consider the default constructor to be part of the public interface for collections. You can declare a variable named collection to be an instance of TArrayOf<TCollectibleLong> with the following statement.
TArrayOf<TCollectibleLong> collection(NIL, NIL);
TArrayOf<TCollectibleLong> *collection;
collection = new TArrayOf<TCollectibleLong>(NIL, NIL);
TArrayOf<TCollectibleLong>* collection = new TArrayOf<TCollectibleLong>(NIL, NIL);