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 <型別 形參表>可以包含基本資料型別可以包含類型別。
請看以下程式:
- #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"
- usingnamespace 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>
- usingnamespace 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.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