ILE C/C++ Programmer's Guide


Example of a Class Template Instantiation

In the following example, the class template Stack implements a stack of items. The overloaded operators << and >> are used to push items onto the stack and pop items from the stack.

Figure 317. Example of Class Template Instantiation


template <class Item, int size> class Stack {
  public:
      int operator << (Item item);  // push operator
      int operator >> (Item& item); // pop operator
      Stack() { top = 0; }          // constructor defined inline
   private:
      Item stack[size];             // stack of items
      int   top;                    // index to top of stack
};


[ Top of Page | Previous Page | Next Page | Table of Contents ]