C M S C     2 1 4
C o m p u t e r   S c i e n c e   I I
F a l l   2 0 0 2


Writing Template Classes

Introduction

When you learned to write functions, the goal was to allow you to plug in different variables on the same code. For example, you might need to square a number. So you call square( x ) or square( y ) or square( arr[ 0 ][ 1 ] ).

Rather than repeat the code in the function over and over, and making substitutions for each variable, you write a function, and call it with different variables.

This works fine when you want to plug in variables of different names but the same type.

What if you would like to do the same thing with different types?

For example, all linked list operations basically work the same on Node objects. The only thing that differs is the type of the data in the Node object, and what the comparison operation does on that type.

Wouldn't it be neat to generalize on the type, so you don't have to write the same code over and over again, where the only change is the type?

That's the purpose of templates. To allow you to write the code once and parameterize the type.

The general approach to templatizing is to have an example code before

Converting Header Files

Converting header files to templates is usually fairly easy. Determine the type (or types) that you want to generalize on.

For example, suppose you have a class that stores a dynamic array.

class MovieArrayList {
   Movie * arr ;
   int  _size;

}
Instead of storing just movies, you want to store any type. Then, you add the template prefix to the top.
template <class Data>  // template prefix
class ArrayList {
   Data * arr ;
   int  _size;

}
which consists of the word template then, in angle brackets, the word class (or typename--the two names are equivalent in this context), followed by an identifier. The identifier is merely a variable name, but the variable is actually a type name (thus, why some people use the keyword typename).

Then, everywhere you see Movie, you replace by the typename, Data. Note: Data is not a real class. It is a variable name meant to be instantiated to a real class.

Caution

Sometimes you will generalize on some common type like int or string. Be careful not to blindly substitute the type name for every occurrence of int or string.

For example, if you have a toString() method, you may really intend to return a string even after templatizing. Similarly size() may return an int, even after templatizing.

Nested Classes

Fortunately, you don't have to put a template prefix for nested classes. You only put the prefix on top of the outer class

Adding .cpp at end of file

Read the other template tutorial to determine which method to compile template files. More than likely you will pick method 3, which is the easiest, or method 1 which works on most compilers.

Converting .cpp Files

The easiest way to see a conversion for a .cpp file is to look at a real example. However, the basic rule is this:

SimpleMap

The posting account should have an example of SimpleStringMovieMap which is an associative array that maps strings to Movies, and is NOT templatized, and a version after templatizing called SimpleMap.

Use this as a guide to converting to templatized functions.

Web Accessibility