Template Klassen

Analog für Klassen Definitionen:

#include <iostream>
using namespace std;
template < class T1, class T2> class Pair {
public:
  T1 first;
  T2 second;
  Pair(const T1& x,const T2&y): first(x),second(y){}
};
int main()
{
  Pair <double,int> p1(3.14,7);
  Pair <int,double> p2(9,4.4);
  cout << "p1:" << p1.first << " " << p1.second << endl;
  cout << "p2:" << p2.first << " " << p2.second << endl;
};

Der Pre-Compiler erstellt (Namen nur illustrativ):

class Pair_di {
public:
  double first;
  int second;
  Pair_di(const double & x,const int &y): first(x),second(y){}
};
class Pair_id {
public:
  int first;
.
  Pair_di p1(3.14,7);
  Pair_id p2(9,4.4);
..