Templates
Yuh-Jzer Joung
²ø ¸Î ¿A
Dept. of Information Management
National Taiwan University
Introduction
Templates - easily create a large range of related functions
or classes
q function template - the blueprint of the related functions
q template function - a specific function made from a function
template
Spring, 2002 Operator Overloading 2
Function Templates
overloaded functions
q perform similar operations on different data types
function templates
q perform identical operations on different data types
q provide type checking
Format:
q template
q can use class or typename - specifies type parameters
Ø template
Ø template
Ø template
q Function definition follows template statement
Spring, 2002 Operator Overloading 3
Function Templates (II)
1 template
2 void printArray( const T *array, const T is the type parameter.
int count )
T's type is detected and
3 {
substituted inside the
4 for ( int i = 0; i
4
5 using std::cout;
6 using std::endl;
7
8 template
9 void printArray( const T *array, const int count )
10{
11 for ( int i = 0; i
class ClassName{
definition
}
q Need not use "T", any identifier will work
q To create an object of the class, type
ClassName myObject;
Ø Example: Stack doubleStack;
Spring, 2002 Operator Overloading 8
Class Templates (II)
Template class functions
q declared normally, but preceded by template
Ø generic data in class listed as type T
q binary scope resolution operator used
q Template class function definition:
template
MyClass::MyClass(int size)
{
myArray = new T[size];
}
q constructor definition - creates an array of type T
Spring, 2002 Operator Overloading 9
1 // Fig. 12.3: tstack1.h
2 // Class template Stack
3 #ifndef TSTACK1_H
4 #define TSTACK1_H
5
6 template
7 class Stack {
8 public:
9 Stack( int = 10 ); // default constructor (stack size 10)
10 ~Stack() { delete [] stackPtr; } // destructor
11 bool push( const T& ); // push an element onto the stack
12 bool pop( T& ); // pop an element off the stack
13private:
14 int size; // # of elements in the stack
15 int top; // location of the top element
16 T *stackPtr; // pointer to the stack
17
18 bool isEmpty() const { return top == -1; } // utility
19 bool isFull() const { return top == size - 1; } // functions
20}; 1. Class template definition
21
22// Constructor with default size 10 a member function of the
Notice how
23template 1.1 Function definitions
class template is defined
24Stack::Stack( int s )
25{
26 : 10;
size = s > 0 ? s 1.2 Stack constructor
27 top = -1; // Stack is initially empty
28 stackPtr = new T[ size ]; // allocate space for elements
29}
30
31// Push an element onto the stack
32// return 1 if successful, 0 otherwise
33template
34bool Stack::push( const T &pushValue )
35{
36 if ( !isFull() ) {
37 stackPtr[ ++top ] = pushValue; // place item in Stack
38 return true; // push successful
39 } Test if the stack is full. If
40 return false; not, push
// push unsuccessful element.
41}
42
43// Pop an element off the stack
44template
45bool Stack::pop( T &popValue )Test if the stack is empty.
46{ 1.3 pushIf not, pop an element.
47 if ( !isEmpty() ) {
48 popValue = stackPtr[ top-- ]; // remove item from Stack
49 return true; // pop 1.4 pop
successful
50 }
51 return false; // pop unsuccessful
52}
53
54#endif
55// Fig. 12.3: fig12_03.cpp
56// Test driver for Stack template
57#include
58
59using std::cout;
60using std::cin;
61using std::endl;
62
63#include "tstack1.h"
64
65int main()
66{
67 Stack doubleStack( 5 );
68 double f = 1.1;
69 cout intStack;
85 int i = 1;
86 cout
Stack mostRecentSalesFigures;
Ø declares object of type Stack
q This may appear in the class definition:
T stackHolder[ elements ]; //array to hold stack
Ø creates array at compile time, rather than dynamic allocation at
execution time
Spring, 2002 Operator Overloading 15
Class Templates and Non-type
Parameters (II)
Classes can be overridden
q for template class Array, define a class named
Array
q This new class overrides then class template for
myCreatedType
q The template remains for unoverriden types
Spring, 2002 Operator Overloading 16
Templates and Inheritance
A class template can be derived from a template class
A class template can be derived from a non-template class
A template class can be derived from a class template
A non-template class can be derived from a class template
Spring, 2002 Operator Overloading 17
Templates and friends
friendships allowed between a class template and
q global function
q member function of another class
q entire class
friend functions
q inside definition of class template X:
q friend void f1();
Ø f1() a friend of all template classes
q friend void f2( X & );
Ø f2( X & ) is a friend of X only. The
same applies for float, double, etc.
q friend void A::f3();
Ø member function f3 of class A is a friend of all template
classes
Spring, 2002 Operator Overloading 18
Templates and friends (II)
q friend void C::f4( X & );
Ø C::f4( X & ) is a friend of class
X only
friend classes
q friend class Y;
Ø every member function of Y a friend with every template class
made from X
q friend class Z;
Ø class Z a friend of class X, etc.
Spring, 2002 Operator Overloading 19
Templates and static Members
non-template class
q static data members shared between all objects
template classes
q each class (int, float, etc.) has its own copy of static data
members
q static variables initialized at file scope
q each template class gets its own copy of static member
functions
Spring, 2002 Operator Overloading 20