Templates in C++ - defining a family pattern of methods / functions
In C, function names need to be unique. If you try to compile or load two functions of the same name, even with a different number / type of parameters, you'll get an error rather than a runnable file out of your compile / load process. See [here]
In C++, you can have methods (functions) in each class (namespace) of the same name - and indeed this is the whole basis of polymorphism, where the piece of code to be run when the program's in operation is selected dynmaically based on the type of data on which it's run. This can even change from one iteration of a loop to another, so that if you have an array that contains fishing rods and skis, you can call different "getlength" pieces of code on each of them without having to use any if
type constructs. And in C++, you can also gave multiple methods / functions of the same name, each with a different number / type of parameters, and the compiler, loader and runtime library will sort them out for you, calling the right one an the right time. Example [here]
, with minimal changes from the C example above.
If you require a whole family
of functions in C++, you may use a function template
. There's an example [here]
on our web site.
Templates are essentially patterns - you can say that there is a whole pattern of methods which is available if you call a function of a certain name, with differing data types in use. The example that I've linked to above defines a function called "exchange" which swaps over two variables ... and needs a third variable internally of the same type
to act as a temporary store.
Sample template definition:
void exchange(T& x, T& y)
T tmp = x;
x = y;
y = tmp;
Templates can have several differing types in their call, and you can even define multiple templates of the same name, with a generic one initially then exception(s) where a particular type is passed in as a parameter. In an extended examle [here]
, we have modified our "exchange" template to give it functionally different behaviour via an alternative template if it's called with bool
Also in that example, we've used a template to give a variable return type too, and to force the incoming parameters to be specified / cast too:
double gloucester = differ<float, long int, double>(boat,length);
Looking at that notation, you'll spot that standard library code is in fact template based too, with the C++ vector
, for example, using a template to allow the vector to hold data of a type of your choosing. (written 2011-08-12, updated 2011-08-13)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesC238 - C and C based languages - Templates 
Using a vector within an object - C++ - (2013-01-19) 
Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21) 
Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06) 
C++ - unknown array size, unknown object type. Help! - (2011-04-17) 
Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12) 
Some new C programming examples - files, structs, unions etc - (2007-12-19)
Some other Articles
What costs 8.20 from Melksham, or 22.30 via Chippenham?For programmers who use Internet Explorer as their browserPrinting objects in C++Plenty to do in MelkshamTemplates in C++ - defining a family pattern of methods / functionsEating out in Melksham - where we like for lunch.Adding the pieces together to make a complete language - CDo university courses teach the right things for life at work later on?Loops - a comparison of goto, while and forAre people who walk into Melksham being asked to subsidise parking?
4300 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).