The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Templates and Classes

{templates!in classes}

Let's say that rather than creating a puny little templated function, you would rather use templates in a class, so that the class may handle more than one datatype. If you've noticed, pmatrix and pvector are both able to handle creating matrices and vectors of int, double, and etc. This is because there is a line, template <class SOMETYPE> in the line preceding the declaration of the class. Just take a look:

template <class SOMETYPE>
class pmatrix {
  ...
  ...
  ...
};

If you want to declare a function that will return your typeparameter then replace the return type with your typeparameter name.

template <class SOMETYPE>
SOMETYPE printFunction();


Last Update: 2005-12-05