3.5. The Utility Classes

The utility classes store and process information for you. They are template classes.

3.5.1. Templates

One of the newer features of C++ are templates. In a template class, the same code can handle any data type. That means you don't have to rewrite your code for all data types you might want to handle. Listing 3.9 shows a sample template class called MyList. MyClass below handles both int and char values:


Example 3.9. stl.cpp: A Program That Shows How Template Classes Work

   1 
   2  1: #include <iostream.h>
   3  2: 
   4  3: template <class C> class MyList
   5  4: {
   6  5:   C list[2];
   7  6:   public:
   8  7:     MyList(void) { }
   9  8:     void insert(int index, C item){ list[index] = item; }
  10  9:     C get(int index){ return list[index]; }
  11 10: };
  12 11: 
  13 12: int main()
  14 13: {
  15 14:   MyList<int> list1;
  16 15:   list1.insert(0, 43);
  17 16:   list1.insert(1, 14);
  18 17:   cout << list1.get(0) << list1.get(1) << endl;
  19 18: 
  20 19:   MyList<char> list2;
  21 20:   list2.insert(0, 'a');
  22 21:   list2.insert(1, 'b');
  23 22:   cout << list2.get(0) << list2.get(1) << endl;
  24 23: 
  25 24:   return 0;
  26 25: }
  27 

The template class MyList can store values of any type. The preceding example demonstrates that it can store both char and int values.

3.5.2. Standard Template Library (STL)

STL stands for Standard Template Library. STL is part of C++. It is a set of standard templates. These templates were created because they solve common problems such as storing values.

You can use STL in your KDE programs, although it's not recommended. Many template classes are available in Qt and KDE; you should use those instead of the STL classes if you can. The Qt and KDE classes are made especially for Qt and KDE programs.

3.5.3. QList—A Qt Template Class

This class is a Qt template class. It maintains lists. The items in the lists can be of any type.

The lists are so-called linked lists. This means that each list item stores a reference to the previous and the next list items.

3.5.3.1. Important Member Functions

To append an item to the list, use the following:


   1 
   2 void append(const type *item)
   3 

To get the first item in the list, use the following:


   1 
   2 type *first()
   3 

The first item becomes the current item.

To get the next item in the list, use


   1 
   2 type *next()
   3 

The next item becomes the current item.

3.5.3.2. Sample Use of QList

Listing 3.10 shows how to use QList. I have created a simple class called MyClass. I use QList to keep a list of MyClass objects.


Example 3.10. qlist.cpp: QList Example

   1 
   2  1: #include <qlist.h>
   3  2: #include <iostream.h>
   4  3: 
   5  4: class MyClass{
   6  5: public:
   7  6:   MyClass() { t=0; }
   8  7:   int get() { return t++; }
   9  8: private:
  10  9:   int t;
  11 10: };
  12 11: 
  13 12: 
  14 13: int main()
  15 14: {
  16 15:   QList<MyClass> list;
  17 16:   MyClass *temp;
  18 17: 
  19 18:   // Delete all list items, when the list is deleted.
  20 19:   list.setAutoDelete(TRUE);
  21 20: 
  22 21:   for (int i=0;i<3;i++)
  23 23:     // Create a list item
  24 24:     temp = new MyClass;
  25 25: 
  26 26:     // Append the list item to the list
  27 27:     list.append(temp);
  28 28: }
  29 29: 
  30 30:   // Call the member function get() in every list item,
  31 31:   // and print the result on the screen.
  32 32:   for (temp = list.first(); temp != 0; temp=list.next())
  33 33:     cout << temp.get() << endl;
  34 34: 
  35 35:   return 0;
  36 36: }
  37 

The list items in QList can be of any type. One possible use of QList is in a MDI program to keep a list of pointers to the windows.