Code About Us Tutorial


  Templates
    Template Functions
    Multiple Template Parameters
    Template Specialization
    Class Templates
    Linked List
    Linked List Template
    Binary Tree


   
 

Home > Templates > Template Functions

November 28, 2009 4:55 am

   

Template Functions

The functions or classes created by the template are called instances of the template. Here is the syntax of the template:

 

 
template <class type parameter>

Templates are part of the new ANSI-C++ standard. If you use a C compiler or a C++ compiler that is not recent, you will not have the advantage of using templates. Templates are not compiled in the code unless you instantiate an instance of the function, and when you do instantiate them, they will be compiled only once for each type that you use. You always start off a template with the keyword "Template." Then type the keyword class and the type parameter. The type parameter is a placeholder that will change with each different instance. There are two ways that are common ways of making template functions, one is the use of "class" and the other is "typename". We will be using "class" definition of template, as it is the most commonly used in the language. Here is an example:

 

 
template <class T>
void swap(T& rx, T& ry)
{
	T temp = rx;
	rx = ry;
	ry = temp;
}

The above example uses a template with a function. I named the type parameter T, because it reminds me of the word "template." But you could use any word or letter you would like. The above will not work on strings since strings are internally an array of chars and if you pass a string to a function you use the pointer of the first element of the array. Therefore, the above will not work on strings. We will need a separate template for it that uses pointers instead of references. I will come back to that in a while. Because the swap function uses a template of only one type (or class), calling it with two different types will NOT work. Please Rate this Code:


    Comments for: Template Functions
Annonymouse User says:
I like this, it really helped me.

Annonymouse User says:
I like this, it really helped me. says:
i hate this, it didn\'t help me at all. you suck.

Annonymouse User says:
I like this, it really helped me. says:
i hate this, it didn\'t help me at all. you suck. says:
I LIKE THIS IT IS COMPLETELY UNHELPFUL YOU SUCK IT\'S GOOD.

stupered [asdf@asdf.asdf] Posted: 2 times. says:
you really don\'t know what you\'re talking about

stupered [asdf@asdf.asdf] Posted: 2 times. says:
you really don\'t know what you\'re talking about says:
its helpful for novice like myself thanks

stupered [asdf@asdf.asdf] Posted: 2 times. says:
you really don\'t know what you\'re talking about says:
its helpful for novice like myself thanks says:
THis sucks.Big time!

stupered [asdf@asdf.asdf] Posted: 2 times. says:
you really don\'t know what you\'re talking about says:
its helpful for novice like myself thanks says:
THis sucks.Big time! says:
lol u guys are funny

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Stop Contradicting yourself, this page is good.

A Note

Templates could get quite confusing, but remember why you are using them. If you are unsure, just a standard overload will save you time.

width=1
width=1

-MoMad (the Author)

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Stop Contradicting yourself, this page is good.

A Note

Templates could get quite confusing, but remember why you are using them. If you are unsure, just a standard overload will save you time.

width=1
width=1

-MoMad (the Author) says:
how to use tamplate in polymorphysm.... i'm confused with "new" operator... could u giv me som example....thank's before

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Stop Contradicting yourself, this page is good.

A Note

Templates could get quite confusing, but remember why you are using them. If you are unsure, just a standard overload will save you time.

width=1
width=1

-MoMad (the Author) says:
how to use tamplate in polymorphysm.... i'm confused with "new" operator... could u giv me som example....thank's before says:
this is coooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooool





Add Comment:
Name:
Email:


 «1» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.