DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
A List Class Library for C++ - List(C++)

List declarations

Every compilation that uses a List must contain the line:

   #include <List.h>

near the beginning of the file. This defines templates that are used to declare and implement Lists. As mentioned above, each List contains only one type of object. If you want to have Lists of integers and Lists of Strings in your program, you must declare automatic, static, and external variables of types List<int> and List<String>. Henceforth in this tutorial, we will use the letter T to represent the type of the object on a List, which is int for List<int> and String for List<String>.

Lists of pointers are like Lists of objects in that a pointer is a kind of object. Declarations for Lists that hold pointers can be of the form: List<T*>.

However, there is a special template for creating Lists of pointers that is a little more convenient to use and generates a lot less code. For example, to create a List of pointers to floats, you can declare variables of type List_of_p<float>. The set of operations on Lists and List_of_p's are identical.

Any type can be on a List<T>, including your own structs and classes. But the type, T, must have the == operator (T::operator==(const T&);) defined in order for List operations to work.

For example, if you want a List of pointers to functions (that map integers to integers, say) and a List of structs, each containing a String and an int, you might write:

   typedef int (ifunc)(int);
   class Myclass {
       int    count;
       String  word;
   public:
       Myclass();
       Myclass(const Myclass&);
      ~Myclass();
       int operator==(const Myclass&);
       Myclass& operator=(const Myclass&);
   }; 

to create the types List_of_p<ifunc> and List<Myclass>.

The initial value of a List declaration is an empty List, unless an explicit initialization is given. Initializers can be either Lists or List elements. For example,

   1:    List<int>      w;
   2:    List<int>      x(2);
   3:    List<String>   y("abcd", "efg");
   4:    List<String>   z = y;

are all legal List declarations. In line 1, w is initialized to an empty List of integers. In line 2, x is initialized to a List of integers with one element, 2. In line 3, y is initialized to a two-element List of Strings (which were themselves initialized from arrays of characters). You can supply up to four elements in a List initializer. In line 4, z is initialized to a copy of y.

Storage management of List elements is handled by the List functions themselves, but if you build a List of pointers, it is up to you to be sure that the pointed-to objects are valid when the pointers are dereferenced.


Next topic: Operations on Lists
Previous topic: What is a List?

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005