1. 程式人生 > >C++中模板與用C語言中實現模板

C++中模板與用C語言中實現模板

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 <型別 形參表>可以包含基本資料型別可以包含類型別。

請看以下程式:

  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.      int n1=2,n2=10;  
  14.      double d1=1.5,d2=5.6;  
  15.      cout<< "較小整數:"<<min(n1,n2)<<endl;  
  16.      cout<< "較小實數:"<<min(d1,d2)<<endl;  
  17.      system("pause");  
  18. }  

程式執行結果:

1 、模板類和過載函式一起使用
     兩者一起使用時,先考慮過載函式,後考慮模板類,如過再找不到,就考慮型別轉換,可能會帶來精度的變化。

  1. #include "iostream"
  2. usingnamespace std ;  
  3. //函式模板 
  4. template <class T>    
  5. const T MAX(T a , T b)  
  6. {  
  7.     printf("%s\n" , "template") ;  
  8.     return (a > b) ? a : b ;       
  9. }  
  10. int MAX(int x , int y)  
  11. {  
  12.     printf("%s\n" , "int int" );  
  13.     return (x > y) ? x : y ;       
  14. }   
  15. int MAX(char x , int y)  
  16. {  
  17.     printf("%s\n" , "char int" );  
  18.     return (x > y) ? x : y ;       
  19. }   
  20. int MAX(int x , char y)  
  21. {  
  22.     printf("%s\n" , "int char" );  
  23.     return (x > y) ? x : y ;      
  24. }   
  25. int main(void)  
  26. {    
  27.     int a = 3 , b = 5 ;  
  28.     char x = 'x' ;   
  29.     double c = 3.4  ;  
  30.     cout<<MAX(a , b)<<endl ;     //呼叫過載函式 
  31.     cout<<MAX(c , b)<<endl ;     //無對應的過載函式,則呼叫模板 
  32.     cout<<MAX(a , x)<<endl ;     //過載函式 
  33.     cout<<MAX(x , a)<<endl ;     //過載函式 
  34.     cout<<MAX(c , a)<<endl ;  
  35.     cout<<MAX(a) ;  
  36.     system("pause") ;   
  37.     return 0 ;     
  38. }  

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

       }
      在類定義體外定義的成員函式,應該使用函式模板。

  1. /* 
  2. 類模板,但是在類外定義成員函式的時候,需要使用函式模板 
  3. */
  4. #include <iostream>
  5. usingnamespace 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. template <class T>   //模板在類外的定義 
  19. void  Base<T>::setA(T c)  
  20. {  
  21.     a = c ;  
  22. }  
  23. int main(void)  
  24. {  
  25.     Base <int>b(4);  
  26.     cout<<b.getA()<<endl;   
  27.     Base <double> bc(4);  
  28.     bc.setA(4.3);  
  29.     cout<<bc.getA()<<endl;   
  30.     system("pause");   
  31.     return 0 ;      
  32. }  

注意成員函式在類外定義的情況。
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