All Sessions - Lesson 15 (partial) We are not going to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lesson 15 (partial) We are not going to cover macros in this course. An Introduction to Templates Templates are probably one of the most powerful features of the C++ language that often are the least approached, or understood. Templates in C++ allow you to define a behavior that you can apply to objects of varying types. Template Declaration Syntax You begin the declaration of a template using the template keyword followed by a type parameter list. The format of this declaration is template <parameter list> … template declaration … Let’s analyze a sample template function declaration: template < typename objectType> const const objectType & value1, const objectType { if (value1 > value2) return value1; else return value2; } The keyword template marks the start of a template declaration and is followed by the template parameter list. This parameter list contains the typename keyword that defines the template parameter objectType, making it a placeholder for the type of the object that the template is being instantiated for. The template declaration is what contains the pattern that you want to implement. Sample usage: #include <iostream> using namespace std; int main() { int nInteger1 = 25; int nInteger2 = 40; const int nMaxValue = GetMax < int > (nInteger1, nInteger2); cout << "The largest of " << nInteger1 << " and " << nInteger2 << " is " << nMaxValue << endl; double dDouble1 = 1.1; double dDouble2 = 1.001; const double dMaxValue = GetMax < double > (dDouble1, dDouble2); cout << "The largest of " << dDouble1 << " and " << dDouble2 << " is " << dMaxValue << endl; return 0; } Page 1 of 18
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Note the detail <int> used in the call to GetMax. It effectively defines the template parameter objectType as int. The preceding code would lead to the compiler generating two versions of the template function GetMax that can be visualized as the following: GetMax ( const int & value1, const int { //. .. } GetMax ( const double & value1, const double { //. .. } In reality, however, template functions don’t necessarily need an accompanying type specifier. So, the following function call would work perfectly well: const int nMaxValue = GetMax (nInteger1, nInteger2); Compilers in this case are intelligent enough to understand that the template function being invoked for the integer type. However, this is true only for template functions and not for classes. The Different Types of Template Declarations A template declaration can be - A declaration or definition of a function (as you saw earlier) - A declaration or definition of a class - A definition of a member function or a member class of a class template - A definition of a static data member of a class template - A definition of a static data member of a class nested within a class template - A definition of a member template of a class or class template Template Classes Template classes are templatized versions of C++ classes that you have learned since
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/07/2010 for the course CS 175 taught by Professor Mike during the Spring '06 term at Golden West College.

Page1 / 18

All Sessions - Lesson 15 (partial) We are not going to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online