[
Example 1:
It is possible for a single class template
List
to provide an unbounded set of class definitions:
one class
List<T> for every type
T,
each describing a linked list of elements of type
T. Similarly, a class template Array describing a contiguous,
dynamic array can be defined like this:
template<class T> class Array {
T* v;
int sz;
public:
explicit Array(int);
T& operator[](int);
T& elem(int i) { return v[i]; }
};
The prefix
template<class T>
specifies that a template is being declared and that a
type-name T
can be used in the declaration
. In other words,
Array
is a parameterized type with
T
as its parameter
. —
end example]