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 }; |
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.