Difference between revisions of "CPP/Templates/ClassTemplateExplicitInstantiation"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Point.cpp)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
==ClassTemplate.cpp==
+
==ClassTemplateExplicitInstantiation.cpp==
 
<source lang="cpp">
 
<source lang="cpp">
 
#include <iostream>
 
#include <iostream>
#include <vector>
 
  
 
#include "Point.h"
 
#include "Point.h"
 
using namespace std;
 
  
 
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
 
#include <vector>
 
 
using namespace std;
 
  
 
template <typename T>
 
template <typename T>
 
class Point
 
class Point
 
{
 
{
T x,y,z;
+
  T x,y,z;
  
 
public:
 
public:
double Add();
+
  double Add();
 
};
 
};
 
#ifdef NO_EXPLICIT_INSTANTIATION
 
 
//must put the definition here to avoid explicit instantiation
 
template <typename T>
 
double Point<T>::Add()
 
{
 
return 2.0 + 4.3;
 
}
 
 
#endif
 
  
 
#endif
 
#endif
Line 54: Line 36:
 
#include "Point.h"
 
#include "Point.h"
  
#include <vector>
 
 
using namespace std;
 
 
#ifndef NO_EXPLICIT_INSTANTIATION
 
 
//can put the definition here, however... (see below)
 
 
template <typename T>
 
template <typename T>
 
double Point<T>::Add()
 
double Point<T>::Add()
 
{
 
{
return 2.0 + 4.3;
+
    return 2.0 + 4.3;
 
};
 
};
  
//... you will be required to instantiate all classes you might use from this template.
+
// 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>;
//... etc.
+
</source>
  
 +
==CMakeLists.txt==
 +
<source lang="cmake">
 +
Project(ClassTemplateExplicitInstantiation)
  
#endif
+
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)