Class Templates


A class template specifies a family of classes. A particular class can be generated by supplying names for the template parameters. The syntax for doing this is shown below.

Syntax

template_class_name:
       template_name <template_parameter_list>
 
template_parameter_list:
       template_parameter
       template_parameter_list, template_parameter
 
template_parameter:
       type_name
       expression

Notes

Example

A matrix template may be required to support both real and complex numbers. Following is an example of such a class. The class parameter of the template (T) may be of the type number or of type complex (say). The two non-class parameters, rows and columns are of integer type and define the dimensions of the matrix. Note that an expanded notation is used for some of the C++ keywords.

template<class T,unsigned rows,unsigned columns>
class matrix
{
 public:
  T elements[rows][columns];

  matrix();

  matrix(constant T* constant);

  matrix operator+() constant;
  matrix operator-() constant;

  integer operator==(constant matrix&) constant;
  integer operator!=(constant matrix&) constant;

  matrix operator+(constant matrix&) constant;
  matrix operator-(constant matrix&) constant;

  matrix<T,rows,rows> operator*(constant matrix<T,columns,rows>&) constant;

  matrix& operator+=(constant matrix&);
  matrix& operator-=(constant matrix&);

  vector<T,rows> operator*(constant vector<T,columns>&) constant;

  matrix operator*(constant T&) constant;
  matrix operator/(constant T&) constant;

  matrix& operator*=(constant T&);
  matrix& operator/=(constant T&);

  operator constant T*() constant;

  vector<T,columns>& operator[](integer row);

  T& operator()(unsigned row,unsigned column);

  unsigned rows() constant;
  unsigned columns() constant;
  unsigned size() constant;
};
....
template<class T, unsigned rows, unsigned columns>
matrix<T,rows,columns> matrix<T,rows,columns>::operator+(constant matrix& m) constant
{
 matrix r;

 for (unsigned i=0; i<rows; i++)
  for (unsigned j=0; j<columns; j++)
   r.elements[i][j] = elements[i][j] + m.elements[i][j];

 return r;
}

template<class T, unsigned rows, unsigned columns>
matrix<T,rows,rows> matrix<T,rows,columns>::operator*(constant matrix<T,columns,rows>& m) constant
{
 matrix<T,rows,rows> r;

 for (unsigned i=0; i<rows; i++)
  for (unsigned j=0; j<rows; j++)
   for (unsigned k=0; k<columns; k++)
    r.elements[i][j] += elements[i][k] * m.elements[k][j];

 return r;
}

Two of the methods (addition and multiplication) are declared within the class template and defined immediately thereafter. For addition, once within the scope of the method, the template name 'matrix' may be used without further qualification. Hence, the parameter for addition is declared as:

matrix<T,rows,columns> matrix<T,rows,columns>::operator+(constant matrix& m) constant

instead of the lengthier declaration shown below.

matrix<T,rows,columns> matrix<T,rows,columns>::operator+(constant matrix<T,rows,columns>& m) constant

As may be observed, this simplification has been used extensively within the class declaration. In the case of multiplication, full qualification is required because the dimensions of the matrix are reversed.

Methods of a class template are implicitly function templates. Function templates may also be defined in support of the class template.