Difference between revisions of "CPP/Templates/PartialClassSpecialization"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Point.h)
 
Line 7: Line 7:
 
int main()
 
int main()
 
{
 
{
   Point<int, int, double> a;
+
   Point<int, double> a;
   Point<int, int*, 5> b;
+
   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();
Line 33: Line 34:
 
class Point<T, int>
 
class Point<T, int>
 
{
 
{
 +
public:
 
   void function1();
 
   void function1();
 
};
 
};
  
#include "Point.txx"
 
 
template<typename T, typename U>
 
template<typename T, typename U>
 
void Point<T,U>::function1()
 
void Point<T,U>::function1()
Line 56: Line 57:
 
}
 
}
 
#endif
 
#endif
 
 
</source>
 
 
==Point.txx==
 
<source lang="cpp">
 
 
 
</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)