C++模板类的使用和继承
来源:互联网 发布:空姐收入 知乎 编辑:程序博客网 时间:2024/06/09 10:57
定义一个类模板,注意两点:
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 oncetemplate <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){}
0 0
- C++模板类的使用和继承
- Flask中render_template的使用和模板的继承
- Flask中render_template的使用和模板的继承
- Flask中render_template的使用和模板的继承
- 模板类的继承
- 类模板的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- 模板类的继承
- Prefab与Instance Unity3d 圣典 用户手册
- Java开发工具及组件应用视频教程
- ubuntu 彻底删除并重装数据库mysql
- eventhub
- 利用python爬虫抓取OJ上做题信息(扩展版)
- C++模板类的使用和继承
- C++语言的15个晦涩特性
- Struts2 学习笔记 —— 06 —— ActionWildCard 通配符
- spring 静态工厂 &实例工厂实例方法 & 实现factorybean接口
- 如何把菜单添加到另外一个VSPackage的菜单里?
- String.Trim()方法总结
- 依赖倒转 -- 针对接口编程
- 北京:地铁春运16日开始 客流将同比增长一成
- WCE驱动的测试-----在XXX_Init里测试