How to use dynamic arrays

This document describes how to create templated arrays; that is, an array of a particular type of object.

All array classes are templated; the template value defines the type of object which is to form an element of the array. This is true for arrays of same length and variable length elements. The following example code constructs an array of same length elements of type TMyClass using the flat array buffer CArrayFixFlat<class T> . The array has granularity 16:

      
       
      
      CArrayFixFlat<TMyClass> array;
...
array = new (ELeave) CArrayFixFlat<TInt>(16);
...
     

To construct an array of TInt values with a granularity of 16, implement code as follows:

      
       
      
      CArrayFixFlat<TInt>* array;
...
array = new (ELeave) CArrayFixFlat<TInt>(16);
     

After construction, the array contains no elements and no memory is allocated to the array buffer.

CArrayPtrFlat is a specialised array where the elements are pointers to CBase derived objects. To construct an array whose elements are pointers to CMyClass objects:

      
       
      
      CArrayPtrFlat<CMyClass>* array;
...
array = new (ELeave) CArrayPtrFlat<CMyClass>(16);
     

To construct an array of same length elements of type TMyClass using RArray<class T> :

      
       
      
      RArray<TMyClass> array; // this is on the stack
...
...                     // add entries etc
...
array.Close();          // called before the array goes out of scope
     

To construct an array of pointers to CMyClass objects using RPointerArray<class T> :

      
       
      
      RPointerArray<CMyClass> array;  // this is on the stack
...
CMyClass* ptr = new (ELeave) CMyClass;
...
array.Append(ptr);
...
array.ResetAndDestroy();       // called before the array goes
                               // out of scope