88 NOVEMBER 2007 | LINUX FOR YOU | www.
linuxforu.
com
C M Y K
Overview
his article is a follow-up to the
‘An Introduction to C++
Templates’ article published in
the October issue of LFY.
So,
without further ado, let’s get
started.
Class and function...
More
88 NOVEMBER 2007 | LINUX FOR YOU | www.
linuxforu.
com
C M Y K
Overview
his article is a follow-up to the
‘An Introduction to C++
Templates’ article published in
the October issue of LFY.
So,
without further ado, let’s get
started.
Class and function templates
There are two kinds of templates: function
templates and class templates.
To define a
template, use the template keyword with the
template parameters given within ankle
brackets, and provide the class or function
definition after that.
For example:
// array_container is a ‘class template’
template <class T>
class array_container {
T arr[10];
// other members
};
// generic swap is a ‘function template’
template <class Type>
void swap(Type &t1, Type &t2) {
Type temp = t1;
t1 = t2;
t2 = temp;
}
The conventional names typically used for
template-type arguments are T and Type, but
it can be any valid variable name.
Template instantiation
We can ‘use’ the template by providing
necessary arguments, which is known as
instantiating t
Less