18.3. Function Template to Manipulate a Class-Template Specialization Object

Notice that the code in function main of Fig. 18.2 is almost identical for both the doubleStack manipulations in lines 9–32 and the intStack manipulations in lines 34–56. This presents another opportunity to use a function template. Figure 18.3 defines function template testStack (lines 10–39) to perform the same tasks as main in Fig. 18.2push a series of values onto a Stack<T> and pop the values off a Stack<T>.

 1   // Fig. 18.3: fig18_03.cpp 2   // Passing a Stack template object 3   // to a function template. 4   #include <iostream> 5   #include <string> 6   #include "Stack.h" // Stack class template definition 7   using namespace ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.