| René Nyffenegger's collection of things on the web | |
|
René Nyffenegger on Oracle - Most wanted - Feedback
|
Templates in C++ | ||
t.h
#ifndef __T_H__
#define __T_H__
#include "u.h"
class T {
public:
T() {}
template<class UT>
void print_u(const U<UT>& u) {
u.print();
};
};
#endif
u.h
#ifndef __U_H__
#define __U_H__
template <typename T>
class U {
public:
U(const T& t) : t_(t) {};
void print() const;
private:
T t_;
};
#endif
u.cpp
#include "u.h"
#include <iostream>
using namespace std;
template class U<int>;
template class U<char>;
template <class T>
void U<T>::print() const {
cout << t_ << endl;
}
mainmain.cpp
#include "u.h"
#include "t.h"
int main() {
U<int> u_5(5);
U<char> u_f('f');
T t;
t.print_u(u_5);
t.print_u(u_f);
return 0;
}
See also static members in templates
|