使用模板可以创建可重用的代码。
模板可以分为两类,一个是函数模板(通用函数),另外一个是类模板(通用类)。
函数模板定义了一组应用于不同数据类型的通用运算。
- 使用关键字 template 来创建.
template <class Type> ret-type func-name(parameter list){
//..
}
//或者
template <class Type>
ret-type func-name(parameter list){
//..
}
其中,Type 是一个占位符,代表函数使用的数据类型.
#include <iostream>
using namespace std;
//模板函数: 交换两个变量的值。
template <class X>
void swap(X &a, X &b){
X temp; // 用X来代表通用的数据类型.
temp=a;
a=b;
b=temp;
}
int main(){
int i1=10,i2=20;
double dbl1=3.3,dbl2=8.8;
char ch1='a',ch2='b';
swap(i1,i2);
swap(dbl1,dbl2);
swap(ch1,ch2);
return 0;
}
模板类定义了在类中使用的全部算法。
创建模板类
template <class Ttype> class class-name{
//
}
//或者
template <class Ttype>
class class-name{
//
}
其中 Ttype 是类型名字占位符, 在实例化类的时候将使用指定的数据类型来替换.
如果需要使用多个通用数据类型,使用逗号分割参数列即可.
创建模板类的一个实例
class-name <type>ob;
type 是对象需要使用的数据类型.
模板类的成员函数也将自动成为模板函数,不需要使用template 来声明他们。
======= 补充说明=======
模板函数,再举例: 取<最大值>
template <class Type>
Type MAX(Type a, Type b)
if(a>b)
return a;
else
return b;
}
int main(){
int iMax=MAX<int>(10,12);
long lMax=MAX<long>(10,12);
double dMax=MAX>double>(10.0,12.0);
return 0;
}
当调用模板函数的时候,需要把类型(Data Type) 作为参数提供给函数.
模板是一种编译时(compile-time)结构,并且会根据所指定的不同类型进行扩展。
模板函数提供了一种编写类型安全,可复用代码的优良机制。
模板类提供了一种类似于预处理的替换技术,运行编写可复用的,类型安全的类。
基于模板的堆栈(Stack)类。
- Stack 提供一个LIFO结构,栈里面存储的是相同数据类型 的元素.
- 采用模板的Stack类,可以存储任何类型的数据。
#include <iostream>
using namespace std;
template <class Ttype>
class Stack{
private:
Ttype m_data[100];
short m_sPos;
public:
Stack();
~Stack();
void Push(Ttype value);
Ttype Pop();
bool IsEmpty(){
return(m_sPos==0);
}
bool HasElements(){
return(m_sPos!=0);
}
bool IsFull(){
return(m_sPos==100);
}
};
template <class Ttype>
Stack<Ttype>::Stack(){
m_sPos=0;
}
template <class Ttype>
Stack<Ttype>::~Stack(){
}
template <class Ttype>
void Stack<Ttype>::Push(Ttype value){
m_data[m_sPos++]=value;
}
template <class Ttype>
Ttype Stack<Ttype>::Pop(){
return m_data[--m_sPos];
}
int main(){
//创建double类型栈。
Stack<double> doubleStack;
doubleStack.Push(1.1);
doubleStack.Push(2.0);
doubleStack.Push(3.0);
while(doubleStack.HasElements()){
cout<<doubleStack.Pop()<<endl;
}
//动态创建long类型的栈
Stack<long> *plongStack=new Stack<long>;
plongStack->Push(1000);
plongStack->Push(2000);
while(plongStack->HasElements()){
cout<<plongStack->Pop()<<endl;
}
delete plongStack;
return 0;
}
ATL 如何使用模板?ATL 如何适用模板技术在基类里访问成员函数?
注意,ATL 在覆盖基类特性的时候没有使用虚函数。
如下:CComObject 通过把基类作为一个模板参数进行传递并使用该类型对类实现指针进行类型转换,可以得到指向基类实现的一个直接指针。
#include <iostream>
using namespace std;
class CBase{
public:
CBase(){}
~CBase(){}
void BaseMethod(){
cout<<"BaseMethod in Base"<<endl;
}
};
class CMath:public CBase{
public:
CMath(){}
~CMath(){}
void BaseMethod(){
cout<<"BaseMethod in CMath"<<endl;
}
};
template<class T>
class CComObject:public T{
public:
CComObject(){}
~CComObject(){}
void CallBaseMethod(){
T *pT=static_cast<T*>(this);
pT->BaseMethod();
}
};
int main(){
CComObject<CMath> *pMath=new CComObject<CMath>;
pMath->CallBaseMethod();
delete pMath;
return 0;
}
分享到:
相关推荐
return this 马克-to-win Java视频this指针的详细介绍
C++中按值返回和返回值优化的程序代码
lua-resty模板lua-resty-template是Lua和...带有lua-resty-template的Hello World local template = require " resty.template " -- ORlocal template = require " resty.template.safe " -- return nil, err on errors
Lab2-Return-To-Libc,包含代码、实验说明、相关论文。
这是一个电脑鼠走迷宫(大迷宫)中原路返回的程序,驱动也做过一定的调整,可用于测试
A novel packet format with non-return-to-zero (NRZ) label and duobinary carrier-suppressed return-to-zero (DCS-RZ) payload is proposed for optical packet switching networks. NRZ label is followed by ...
Bypassing non-executable-stack during exploitation using return-to-libc.pdf
复旦大学_软件安全_SEED labs_2-Return_to_libc实验 是从雪城大学SEED labs上找的实验 资源包括:原始文件夹、攻击代码、实验报告详细版
Return to a healthy L-term growth trend.pdf
JSHTML模板 这个gulp插件将带有模板的html文件转换为js文件,该js文件包含一个以所有模板为字符串的对象。 在结构化HTML文件中编写模板比在一行... return gulp . src ( 'my/template.html' ) . pipe ( html
(b) the name of the table, the names of the table's attributes, the data types of the table's attributes, the formats of the table's attributes, and the maximum number of rows that the table can have...
return gulp . src ( 'test/fixtures/*.html' ) . pipe ( gulpEjs ( { moduleName : 'templates' } ) ) . pipe ( gulp . dest ( 'test' ) ) ; } ) ; 演示 test/fixtures/header.html : < p> < %= locals....
| | |安装用安装npm install gulp-angular-templatecache --save-dev例gulpfile.js 连接模板目录中所有.html文件的内容,并保存到public / templates.js (默认文件名)。 var templateCache = require ( 'gulp-...
us-18-Boeck-Young-Return-Of-Bleichenbachers-Oracle-Threat 安全分析 网络信息安全 安全 数据安全 金融安全
gulp-ember-template-compiler 咕嘟咕嘟插件,编译模板灰烬1.12+的适应了这个版本的。安装npm install n-fuse/gulp-ember-template-compiler#1.13.0-beta.1 其中1.13.0-beta.1是要使用的Ember版本。用法 var rename ...
这是一套自用的主题,我对于wordpress的关注已经有很长时间了,一直以来...开发了这套针对设计师与开发者的轻量简洁风格的wordpress主题【return】,在这免费分享给我的设计师与开发者朋友们,同时也为开源社区做点贡献
西兰花琥珀色HBS模板编译器 一种针对Broccoli的自以为是的过滤器,可为ember.js编译车把模板 安装 npm install --save-dev broccoli-ember-hbs-template-compiler 使用范例 module . exports = function ( broccoli...
composer create-project --prefer-dist hoaaah/yii2-rest-api-template [app_name] 从config/db.php设置数据库配置。 创建您的数据库,因为此模板不会为您创建它:) <?php return [ 'components' => [ 'db'...
grunt-ember-template-compiler 入门 如果您以前没有使用过 ,请务必查看指南,因为它解释了如何创建以及安装和使用 Grunt 插件。 熟悉该过程后,您可以使用以下命令安装此插件: ... return sourceFile . repla
吞咽应用模板 将模板应用于文件内容。 对于流中的每个文件,通过渲染模板替换文件的内容,使用文件对象作为模板... return gulp . src ( 'pages/*.html' ) . pipe ( applyTemplate ( { engine : 'swig' , templat