Difference between revisions of "CPP/Templates/PartialClassSpecialization"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==PartialClassSpecialization.cpp== <source lang="cpp"> #include <iostream> #include "Point.h" int main() { Point<int, int, double> a; Point<int, int*, 5> b; a.functio…') |
Daviddoria (Talk | contribs) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 7: | Line 7: | ||
int main() | int main() | ||
{ | { | ||
− | Point< | + | Point<int, double> a; |
− | Point<int, int | + | Point<int, int> b; |
a.function1(); | a.function1(); | ||
Line 26: | Line 26: | ||
class Point | class Point | ||
{ | { | ||
+ | public: | ||
void function1(); | void function1(); | ||
void function2(); | void function2(); | ||
}; | }; | ||
− | template<typename T | + | template<typename T> |
class Point<T, int> | class Point<T, int> | ||
{ | { | ||
+ | public: | ||
void function1(); | void function1(); | ||
− | |||
}; | }; | ||
− | |||
template<typename T, typename U> | template<typename T, typename U> | ||
void Point<T,U>::function1() | void Point<T,U>::function1() | ||
Line 51: | Line 51: | ||
////////////////// | ////////////////// | ||
− | template<class T | + | template<class T> |
void Point<T, int>::function1() | void Point<T, int>::function1() | ||
{ | { | ||
Line 57: | Line 57: | ||
} | } | ||
#endif | #endif | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> | ||
Latest revision as of 10:12, 6 April 2011
PartialClassSpecialization.cpp
#include <iostream> #include "Point.h" int main() { Point<int, double> a; Point<int, int> b; a.function1(); b.function1(); }
Point.h
#ifndef POINT_H #define POINT_H #include <iostream> #include <vector> template<typename T, typename U> class Point { public: void function1(); void function2(); }; template<typename T> class Point<T, int> { public: void function1(); }; template<typename T, typename U> void Point<T,U>::function1() { std::cout << "Primary template function1" << std::endl; } template<typename T, typename U> void Point<T,U>::function2() { std::cout << "Primary template function2" << std::endl; } ////////////////// template<class T> void Point<T, int>::function1() { std::cout << "Partial specialization function1" << std::endl; } #endif
CMakeLists.txt
Project(PartialClassSpecialization) ADD_EXECUTABLE(PartialClassSpecialization PartialClassSpecialization.cpp)