Eccoti un esempio:
Codice PHP:
//
// 3 november 2007
// Compiled and tested with:
// i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
//
#include <iostream>
namespace ig{
template <typename T>
class GTest{
private:
const T fField;
public:
void Dump (void) const;
public:
GTest (const T theField);
GTest (const GTest <T> &theTest);
GTest<T> operator + (const GTest <T> &theTest) const;
};
}
namespace ig{
template <typename T>
GTest<T>::GTest (const GTest <T> &theTest):
fField (theTest.fField){
}
template <typename T>
GTest<T>::GTest (const T theField):
fField (theField){
}
template <typename T>
GTest<T> GTest <T>::operator + (const GTest <T> &theTest) const{
return GTest <T> (fField + theTest.fField);
}
template <typename T>
void GTest<T>:: Dump (void) const{
std::cout << "fField: " << fField << ";" << std::endl;
}
}
int main (int argc, char * const argv[]){
typedef ig::GTest <int> TestInt;
const TestInt aTest1 (1);
const TestInt aTest2 (2);
const TestInt aTestSigma = aTest1 + aTest2;
aTestSigma.Dump (); // it prints out: fField: 3;
return 0;
}