Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Initial Version and Version 1 of code/Template


Ignore:
Timestamp:
Feb 28, 2008, 12:43:06 AM (16 years ago)
Author:
landauf
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/Template

    v1 v1  
     1= Template =
     2
     3== Description ==
     4
     5A [wiki:template] is a class with an undefined type anywhere in it (for example a member variable, a function parameter or return value or even an undefined parent class). This undefined type gets replaced by a symbol. We'll usually use T as the symbol. Of course it's also possible to have more than one undefined type.
     6
     7Possible applications for templates are: Generic lists that can store ints or strings or !MyClasses or other lists, a container that stores an element of a specific class, a [wiki:Factory], a stream and many more.
     8
     9Because the exact specifications of templates aren't defined, the compiler can't just compile the template once. In fact the template gets compiled once in every source that uses the template. That's why it's not possible to just include the header file of a template. You always have to include the source too. This is why programmers usually write the whole code of the template into the header file.
     10
     11Because templates are compiled several times, you should only put the minimum of functions and variables into the template itself and implement everything else in a base class. This has also another advantage: Because a template used with an int and the same template used with a string are two totally different classes, you can't assign them to a common variable. But with a comman base class, this is possible.
     12
     13== Example ==
     14
     15Definition and Implementation of the template:
     16{{{
     17#!cpp
     18// MyTemplate.h
     19
     20class BaseTemplate
     21{
     22  public:
     23    // This function could also be implemented in MyTemplate.cc
     24    void sayHello()
     25      { std::cout << "Hello!" << std::endl; }
     26
     27    ...
     28    lots of functions and variables that don't use T
     29    ...
     30};
     31
     32template <class T>
     33class MyTemplate : public BaseTemplate
     34{
     35  public:
     36    // This function must be implemented here, because it uses T
     37    T add(T var1, T var2)
     38      { return var1 + var2; }
     39};
     40}}}
     41
     42Usage of the template:
     43{{{
     44#!cpp
     45#include "MyTemplate.h"
     46
     47BaseTemplate* base;
     48
     49MyTemplate<int>* template1 = new MyTemplate<int>();
     50std::cout << "Sum: " << template1->add(2, 5) << std::endl;
     51
     52base = template1;
     53base.sayHello();
     54
     55MyTemplate<std::string>* template2 = new MyTemplate<std::string>();
     56std::cout << "Sum: " << template2->add("abc", "def") << std::endl;
     57
     58base = template2;
     59base.sayHello();
     60}}}
     61
     62Output:
     63{{{
     64Sum: 7
     65Hello!
     66Sum: abcdef
     67Hello!
     68}}}
     69}}}