1、模板的概念
我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同。正确的调用重载函数。例如,为求两个数的最大值,我们定义MAX()函数需要对不同的数据类型分别定义不同重载(Overload)版本。
//函数1.
int max(int x,int y)
{ return(x>y)?x:y ; }
//函数2.
float max( float x,float y)
{ return (x>y)? x:y ; }
//函数3.
double max(double x,double y)
{ return (x>y)? x:y ; }
但如果在主函数中,我们分别定义了 char a,b; 那么在执行max(a,b);时 程序就会出错,因为我们没有定义char类型的重载版本。
现在,我们再重新审视上述的max()函数,它们都具有同样的功能,即求两个数的最大值,能否只写一套代码解决这个问题呢?这样就会避免因重载函数定义不 全面而带来的调用错误。为解决上述问题C++引入模板机制,模板定义:模板就是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数,从而实现了真正的代码可重用性。模版可以分为两类,一个是函数模版,另外一个是类模版。
2、 函数模板的写法
函数模板的一般形式如下:
Template <class或者也可以用typename T>
返回类型 函数名(形参表)
{//函数定义体 }
说明: template是一个声明模板的关键字,表示声明一个模板关键字class不能省略,如果类型形参多余一个 ,每个形参前都要加class <类型 形参表>可以包含基本数据类型可以包含类类型。
请看以下程序:
- #include <iostream>
- using std::cout;
- using std::endl;
- //声明一个函数模版,用来比较输入的两个相同数据类型的参数的大小,class也可以被typename代替,
- //T可以被任何字母或者数字代替。
- template <class T>
- T min(T x,T y)
- {
- return(x<y)?x:y;
- }
- void main( )
- {
- int n1=2,n2=10;
- double d1=1.5,d2=5.6;
- cout<< "较小整数:"<<min(n1,n2)<<endl;
- cout<< "较小实数:"<<min(d1,d2)<<endl;
- system("pause");
- }
程序运行结果:
1 、模板类和重载函数一起使用
两者一起使用时,先考虑重载函数,后考虑模板类,如过再找不到,就考虑类型转换,可能会带来精度的变化。
- #include "iostream"
- using namespace std ;
- //函数模板
- template <class T>
- const T MAX(T a , T b)
- {
- printf("%s\n" , "template") ;
- return (a > b) ? a : b ;
- }
- int MAX(int x , int y)
- {
- printf("%s\n" , "int int" );
- return (x > y) ? x : y ;
- }
- int MAX(char x , int y)
- {
- printf("%s\n" , "char int" );
- return (x > y) ? x : y ;
- }
- int MAX(int x , char y)
- {
- printf("%s\n" , "int char" );
- return (x > y) ? x : y ;
- }
- int main(void)
- {
- int a = 3 , b = 5 ;
- char x = 'x' ;
- double c = 3.4 ;
- cout<<MAX(a , b)<<endl ; //调用重载函数
- cout<<MAX(c , b)<<endl ; //无对应的重载函数,则调用模板
- cout<<MAX(a , x)<<endl ; //重载函数
- cout<<MAX(x , a)<<endl ; //重载函数
- cout<<MAX(c , a)<<endl ;
- cout<<MAX(a) ;
- system("pause") ;
- return 0 ;
- }
2 、类模板
(1)类模板的具体格式
template <class T>
class A
{
}
在类定义体外定义的成员函数,应该使用函数模板。
- /*
- 类模板,但是在类外定义成员函数的时候,需要使用函数模板
- */
- #include <iostream>
- using namespace std ;
- template <class T>
- class Base
- {
- public :
- T a ;
- Base(T b)
- {
- a = b ;
- }
- T getA(){ return a ;} //类内定义
- void setA(T c);
- };
- template <class T> //模板在类外的定义
- void Base<T>::setA(T c)
- {
- a = c ;
- }
- int main(void)
- {
- Base <int>b(4);
- cout<<b.getA()<<endl;
- Base <double> bc(4);
- bc.setA(4.3);
- cout<<bc.getA()<<endl;
- system("pause");
- return 0 ;
- }
注意成员函数在类外定义的情况。
3 、模板类
主要指的是 STL 模板类
以上转自:http://blog.youkuaiyun.com/hackbuteer1/article/details/6735704
===================================================================================================
然后在网上又看到了一篇关于用C语言中的宏实现模板的方法如下:
模板是C++中的概念,C语言中没有模板,但在C语言中可以通过宏定义实现类似模板的功能。
例子如下:
FuncTemplate.h包含:
ReturnType Hello( const ElementType& type )
{
ReturnType ret;
CallFunction( ret, type ) // ret = type
return ret;
}
在FuncUsing.c中(即 使用这个模板的C文件)
// 取消定义
#undef ReturnType
#undef ElementType
#undef CallFunction
// 定义“仿真模板”(代替编译器作的工作)
#define ReturnType int
#define ElementType double
#define CallFunction(x,y) ((x) = (y))
#include FuncTemplate.h
...
int hi = Hello( 1.2 ); // hi = 1
注意:要先undef,然后再difine,以免因为在其他文件中使用了同样的功能(并且那个文件被include进来),而产生错误。
以上转自:http://blog.sina.com.cn/s/blog_8b745a5f01014xvz.html