Difference between revisions of "CPP/Templates/ClassTemplateExplicitInstantiation"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) |
Daviddoria (Talk | contribs) |
||
Line 1: | Line 1: | ||
− | == | + | ==ClassTemplateExplicitInstantiation.cpp== |
<source lang="cpp"> | <source lang="cpp"> | ||
#include <iostream> | #include <iostream> | ||
− | |||
#include "Point.h" | #include "Point.h" | ||
− | |||
− | |||
int main(int argc, char* argv[]) | int main(int argc, char* argv[]) | ||
Line 12: | Line 9: | ||
Point<double> A; | Point<double> A; | ||
− | cout << A.Add( ) << endl; | + | std::cout << A.Add( ) << std::endl; |
return 0; | return 0; | ||
} | } | ||
Line 22: | Line 19: | ||
#ifndef POINT_H | #ifndef POINT_H | ||
#define POINT_H | #define POINT_H | ||
− | |||
− | |||
− | |||
− | |||
template <typename T> | template <typename T> | ||
class Point | class Point | ||
{ | { | ||
− | + | T x,y,z; | |
public: | public: | ||
− | + | double Add(); | |
}; | }; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
#endif | #endif | ||
Line 54: | Line 36: | ||
#include "Point.h" | #include "Point.h" | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
template <typename T> | template <typename T> | ||
double Point<T>::Add() | double Point<T>::Add() | ||
{ | { | ||
− | + | return 2.0 + 4.3; | |
}; | }; | ||
− | // | + | // You are required to instantiate all classes you might use from this template |
+ | // since the function is not defined in the header. | ||
template class Point<double>; | template class Point<double>; | ||
template class Point<float>; | template class Point<float>; | ||
− | / | + | </source> |
+ | ==CMakeLists.txt== | ||
+ | <source lang="cmake"> | ||
+ | Project(ClassTemplateExplicitInstantiation) | ||
− | + | ADD_EXECUTABLE(ClassTemplateExplicitInstantiation ClassTemplateExplicitInstantiation.cpp Point.cpp) | |
</source> | </source> |
Latest revision as of 22:38, 29 January 2011
ClassTemplateExplicitInstantiation.cpp
#include <iostream> #include "Point.h" int main(int argc, char* argv[]) { Point<double> A; std::cout << A.Add( ) << std::endl; return 0; }
Point.h
#ifndef POINT_H #define POINT_H template <typename T> class Point { T x,y,z; public: double Add(); }; #endif
Point.cpp
#include "Point.h" template <typename T> double Point<T>::Add() { return 2.0 + 4.3; }; // You are required to instantiate all classes you might use from this template // since the function is not defined in the header. template class Point<double>; template class Point<float>;
CMakeLists.txt
Project(ClassTemplateExplicitInstantiation) ADD_EXECUTABLE(ClassTemplateExplicitInstantiation ClassTemplateExplicitInstantiation.cpp Point.cpp)