Creating an array

To create an array, or any type of collection, you must have an idea of the kinds of elements you intend to put inside. For this release of the CommonPoint application system, elements to be placed in collections must be instances of classes. These may be classes defined by you or provided by CommonPoint. Often you will find that collection elements are instances of classes descending from MCollectible. The dependence of collections on MCollectible will disappear in the future when all C++ compilers used with the CommonPoint system fully support RTTI (Run Time Type Information). In the meaning time, knowing the role MCollectible plays in collections can be helpful.

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)
Use the new operator to create instances of TCollectibleLong suitable for placing inside a collection.

Creating a collection element

        TCollectibleLong *pTCollectibleLong = new TCollectibleLong(3);
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.


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>(NIL, NIL)
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>()    // error: don't use default constructor

Don't use default collection constructors

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);
Or you could declare a pointer to an instance of TArrayOf<TCollectibleLong>

        TArrayOf<TCollectibleLong> *collection;
This instance could then be initialized as follows.

        collection = new TArrayOf<TCollectibleLong>(NIL, NIL);
Good programming practice advocates combining the variable declaration and initialization of collection in a single statement.

          TArrayOf<TCollectibleLong>* collection =
              new TArrayOf<TCollectibleLong>(NIL, NIL);

[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