3.2 Use of Class Template Stack

Ru-Brd

3.2 Use of Class Template Stack

To use an object of a class template, you must specify the template arguments explicitly. The following example shows how to use the class template Stack<> :

  // basics/stack1test.cpp  #include <iostream>  #include <string>  #include <cstdlib>  #include "stack1.hpp"  int main()  {      try {          Stack<int>         intStack;  // stack of  int  s  Stack<std::string> stringStack;  // stack of strings   // manipulate  int  stack  intStack.push(7);          std::cout << intStack.top() << std::endl;  // manipulate string stack  stringStack.push("hello");          std::cout << stringStack.top() << std::endl;          stringStack.pop();          stringStack.pop();      }      catch (std::exception const& ex) {          std::cerr << "Exception: " << ex.what() << std::endl;          return EXIT_FAILURE;  // exit program with ERROR status  }  } 

By declaring type Stack<int> , int is used as type T inside the class template. Thus, intStack is created as an object that uses a vector of int s as elements and, for all member functions that are called, code for this type is instantiated . Similarly, by declaring and using Stack<std::string> , an object that uses a vector of strings as elements is created, and for all member functions that are called, code for this type is instantiated.

Note that code is instantiated only for member functions that are called . For class templates, member functions are instantiated only when they are used. This, of course, saves time and space. It has the additional benefit that you can instantiate a class even for those types that cannot perform all the operations of all the member functions, as long as these member functions are not called. As an example, consider a class in which some member functions use the operator < to sort elements. If you refrain from calling these member functions, you can instantiate the class template for types for which operator < is not defined.

In this example, the default constructor, push() , and top() are instantiated for both int and strings. However, pop() is instantiated only for strings. If a class template has static members , these are instantiated once for each type.

You can use a type of an instantiated class template as any other type, as long as the operations are supported:

 void foo (Stack<int> const& s)  // parameter  s  is  int  stack  {      Stack<int> istack[10];  //  istack  is array of 10  int  stacks    } 

By using a type definition, you can make using a class template more convenient :

 typedef Stack<int> IntStack;  void foo (IntStack const& s)  //  s  is stack of  int  s  {      IntStack istack[10];  //  istack  is array of 10 stacks of  int  s    } 

Note that in C++ a type definition does define a "type alias" rather than a new type. Thus, after the type definition

 typedef Stack<int> IntStack; 

IntStack and Stack<int> are the same type and can be used for and assigned to each other.

Template arguments may be any type, such as pointers to float s or even stacks of int s:

 Stack<float*>      floatPtrStack;  // stack of  float  pointers  Stack<Stack<int> > intStackStack;  // stack of stack of  int  s  

The only requirement is that any operation that is called is possible according to this type.

Note that you have to put whitespace between the two closing template brackets. If you don't do this, you are using operator >> , which results in a syntax error:

 Stack<Stack<int>> intStackStack;  // ERROR:  >>  is not allowed  
Ru-Brd


C++ Templates
C++ Templates: The Complete Guide
ISBN: 0201734842
EAN: 2147483647
Year: 2002
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net