C++中模板与用C语言中实现模板

31 篇文章 2 订阅

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 <类型 形参表>可以包含基本数据类型可以包含类类型。

请看以下程序:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. using std::cout;  
  3. using std::endl;  
  4. //声明一个函数模版,用来比较输入的两个相同数据类型的参数的大小,class也可以被typename代替,  
  5. //T可以被任何字母或者数字代替。  
  6. template <class T>  
  7. T min(T x,T y)  
  8. {  
  9.     return(x<y)?x:y;  
  10. }  
  11. void main( )  
  12. {  
  13.   
  14.      int n1=2,n2=10;  
  15.      double d1=1.5,d2=5.6;  
  16.      cout<< "较小整数:"<<min(n1,n2)<<endl;  
  17.      cout<< "较小实数:"<<min(d1,d2)<<endl;  
  18.      system("pause");  
  19. }  

程序运行结果:

 

 

 

 

 

 

 

1 、模板类和重载函数一起使用
     两者一起使用时,先考虑重载函数,后考虑模板类,如过再找不到,就考虑类型转换,可能会带来精度的变化。

[cpp]  view plain  copy
  1. #include "iostream"  
  2. using namespace std ;  
  3.   
  4. //函数模板   
  5. template <class T>    
  6. const T MAX(T a , T b)  
  7. {  
  8.     printf("%s\n" , "template") ;  
  9.     return (a > b) ? a : b ;       
  10.   
  11. }  
  12.   
  13. int MAX(int x , int y)  
  14. {  
  15.     printf("%s\n" , "int int" );  
  16.     return (x > y) ? x : y ;       
  17. }   
  18.   
  19. int MAX(char x , int y)  
  20. {  
  21.     printf("%s\n" , "char int" );  
  22.     return (x > y) ? x : y ;       
  23. }   
  24.   
  25. int MAX(int x , char y)  
  26. {  
  27.     printf("%s\n" , "int char" );  
  28.     return (x > y) ? x : y ;      
  29. }   
  30.   
  31. int main(void)  
  32. {    
  33.     int a = 3 , b = 5 ;  
  34.     char x = 'x' ;   
  35.     double c = 3.4  ;  
  36.     cout<<MAX(a , b)<<endl ;     //调用重载函数   
  37.     cout<<MAX(c , b)<<endl ;     //无对应的重载函数,则调用模板   
  38.   
  39.     cout<<MAX(a , x)<<endl ;     //重载函数   
  40.     cout<<MAX(x , a)<<endl ;     //重载函数   
  41.     cout<<MAX(c , a)<<endl ;  
  42.     cout<<MAX(a) ;  
  43.     system("pause") ;   
  44.     return 0 ;     
  45. }  

2 、类模板
    (1)类模板的具体格式
        template <class T>
        class A
       {

       }
      在类定义体外定义的成员函数,应该使用函数模板。

[cpp]  view plain  copy
  1. /* 
  2. 类模板,但是在类外定义成员函数的时候,需要使用函数模板 
  3. */  
  4. #include <iostream>  
  5. using namespace std ;  
  6. template <class T>  
  7. class Base  
  8. {  
  9. public :      
  10.     T a ;  
  11.     Base(T b)  
  12.     {  
  13.         a = b ;      
  14.     }     
  15.     T getA(){ return a ;} //类内定义   
  16.     void setA(T c);  
  17. };  
  18.   
  19. template <class T>   //模板在类外的定义   
  20. void  Base<T>::setA(T c)  
  21. {  
  22.     a = c ;  
  23. }  
  24.   
  25. int main(void)  
  26. {  
  27.     Base <int>b(4);  
  28.     cout<<b.getA()<<endl;   
  29.   
  30.     Base <double> bc(4);  
  31.     bc.setA(4.3);  
  32.     cout<<bc.getA()<<endl;   
  33.     system("pause");   
  34.     return 0 ;      
  35. }  

注意成员函数在类外定义的情况。
3 、模板类
  主要指的是 STL 模板类

以上转自:http://blog.csdn.net/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
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值