As shown in the following figure, the constructor function of the template is defined inline. Assume that the other functions are defined using separate function templates in the file stack.c:
Figure 319. Example of a Constructor Function that Is Defined Inline
template <class Item, int size> int Stack<Item,size>::operator << (Item item) { if (top >= size) return 0; stack[top++] = item; return 1; } template <class Item, int size> int Stack<Item,size>::operator >> (Item& item) { if (top <= 0) return 0; item = stack[--top]; return 1; } |
Notes:
Stack<item,size>::operator<<(item)
Stack<item,size>::operator>>(item&)
because there is explicit specialization.
If the class template is instantiated in the source file usrstack.cpp, that C++ program contains code similar to that shown in the following figure:
Figure 320. Example of a Constructor Function that Is Defined Externally
|
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.