定义一个类模板,注意两点:
1,类的定义前面使用关键词:template <class T>
2,函数的实现部分,在每个函数名的上一行也要加关键词template <class T>, 并且在函数名后面添加<T>,例如
template <class T>
ImageProcessor<T>::ImageProcessor(void)
{
}
3,在main.cpp文件中要同时包含模板类的.h文件和.cpp文件。
如果要从一个类模板中继承产生新的类模板注意:
1,子类的头文件中要包含父类的.h文件和.cpp文件。
2,子类的定义中,父类的名称后面要加<T>
以下是一个例子,父类是一个处理图像的通用类,子类在此继承上继承,实现对图像的下采样功能。
模板类的基类为:imageprocessor.h
#pragma once
template <class T>
class ImageProcessor
{
protected:
int input_size[4];
T * input_data;
int output_size[4];
T * output_data;
public:
void setInputImageSize(int, int, int, int);
void setInputData(T *);
void setOutputImageSize(int, int, int, int);
void getOutputImageSize(int []);
void setOutputData(T *);
bool validInputImageSize(void);
bool validOutputImageSize(void);
int getInputPixelPointerShift(int, int,int,int);
int getOutputPixelPointerShift(int, int,int,int);
T* getInputPixelPointer(int, int,int,int);
T* getOutputPixelPointer(int, int,int,int);
T getInputPixelValue(int, int,int,int);
void setOutputPixelValue(int, int,int,int,T);
ImageProcessor(void);
public:
~ImageProcessor(void);
};
.cpp文件为:imageprocessor.cpp
#pragma once
#include "StdAfx.h"
#include "ImageProcessor.h"
#include "windows.h"
#include <iostream>
using namespace std;
template <class T>
ImageProcessor<T>::ImageProcessor(void)
{
}
template <class T>
void ImageProcessor<T>::setInputImageSize(int h,int w,int d,int s)
{
}
template <class T>
void ImageProcessor<T>::setInputData(T* p)
{
}
template <class T>
void ImageProcessor<T>::setOutputImageSize(int h,int w,int d,int s)
{
}
template <class T>
void ImageProcessor<T>::getOutputImageSize(int size[4])
{
}
template <class T>
void ImageProcessor<T>::setOutputData(T* p)
{
}
template <class T>
bool ImageProcessor<T>::validInputImageSize(void)
{
}
template <class T>
bool ImageProcessor<T>::validOutputImageSize(void)
{
}
template <class T>
int ImageProcessor<T>::getInputPixelPointerShift(int x, int y,int z,int s)
{
}
template <class T>
int ImageProcessor<T>::getOutputPixelPointerShift(int x, int y,int z,int s)
{
}
template <class T>
T* ImageProcessor<T>::getInputPixelPointer(int x, int y,int z,int s)
{
}
template <class T>
T* ImageProcessor<T>::getOutputPixelPointer(int x, int y,int z,int s)
{
}
template <class T>
T ImageProcessor<T>::getInputPixelValue(int x, int y,int z,int s)
{
}
template <class T>
void ImageProcessor<T>::setOutputPixelValue(int x, int y,int z,int s, T value)
{
}
template <class T>
ImageProcessor<T>::~ImageProcessor(void)
{
}
子类的.h文件为:ipDownSampler.h
#pragma once
#include "imageprocessor.h"
#include "imageprocessor.cpp"
template <class T>
class ipDownSampler :
public ImageProcessor<T>
{
protected:
double sample_rate[4];
int interpolation_method;
public:
bool setDownSampleRate(double []);
bool getDownSampledSize(void);
bool validDownSampleRate(void);
bool setInterpolationMethod(int);
bool downSample();
T getPixelValueByNearestInterpolation(double,double,double,int);
T getPixelValueByBilinearInterpolation(double,double,double,int);
ipDownSampler(void);
public:
~ipDownSampler(void);
};
其.cpp文件为:ipDownSampler.cpp
#pragma once
#include "StdAfx.h"
#include "ipDownSampler.h"
template <class T>
bool ipDownSampler<T>::setDownSampleRate(double rate[4])
{
}
template <class T>
bool ipDownSampler<T>::getDownSampledSize(void)
{
}
template <class T>
bool ipDownSampler<T>::validDownSampleRate(void)
{
}
template <class T>
bool ipDownSampler<T>::setInterpolationMethod(int method)
{
}
template <class T>
bool ipDownSampler<T>::downSample()
{
}
template <class T>
T ipDownSampler<T>::getPixelValueByNearestInterpolation(double x,double y ,double z,int s)
{
}
template <class T>
T ipDownSampler<T>::getPixelValueByBilinearInterpolation(double x,double y ,double z,int s)
{
}
template <class T>
ipDownSampler<T>::ipDownSampler(void)
{
}
template <class T>
ipDownSampler<T>::~ipDownSampler(void)
{
}