轉自:http://www.360doc.com/content/09/0403/17/799_3011262.shtml
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 (c>y)? x:y ;}
但如果在主函數中,我們分別定義了 char a,b; 那么在執行max(a,b);時 程序就會出錯,因為我們沒有定義char類型的重載版本。
現在,我們再重新審視上述的max()函數,它們都具有同樣的功能,即求兩個數的最大值,能否只寫一套代碼解決這個問題呢?這樣就會避免因重載函數定義不 全面而帶來的調用錯誤。為解決上述問題C++引入模板機制,模板定義:模板就是實現代碼重用機制的一種工具,它可以實現類型參數化,即把類型定義為參數, 從而實現了真正的代碼可重用性。模版可以分為兩類,一個是函數模版,另外一個是類模版。
2. 函數模板的寫法
函數模板的一般形式如下:
Template <class或者也可以用typename T> 返回類型 函數名(形參表) {//函數定義體 }
說明: template是一個聲明模板的關鍵字,表示聲明一個模板關鍵字class不能省略,如果類型形參多余一個 ,每個形參前都要加class <類型 形參表>可以包含基本數據類型可以包含類類型.
請看以下程序:
//Test.cpp #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"); }
程序運行結果:
程序分析:main()函數中定義了兩個整型變量n1 , n2 兩個雙精度類型變量d1 , d2然后調用min( n1, n2); 即實例化函數模板T min(T x, T y)其中T為int型,求出n1,n2中的最小值.同理調用min(d1,d2)時,求出d1,d2中的最小值.
3. 類模板的寫法
定義一個類模板:
Template < class或者也可以用typename T > class類名{ //類定義...... };
說明:其中,template是聲明各模板的關鍵字,表示聲明一個模板,模板參數可以是一個,也可以是多個。
例如:定義一個類模板:
// ClassTemplate.h #ifndef ClassTemplate_HH #define ClassTemplate_HH template<typename T1,typename T2> class myClass{ private: T1 I; T2 J; public: myClass(T1 a, T2 b);//Constructor void show(); }; //這是構造函數 //注意這些格式 template <typename T1,typename T2> myClass<T1,T2>::myClass(T1 a,T2 b):I(a),J(b){} //這是void show(); template <typename T1,typename T2> void myClass<T1,T2>::show() { cout<<"I="<<I<<", J="<<J<<endl; } #endif // Test.cpp #include <iostream> #include "ClassTemplate.h" using std::cout; using std::endl; void main() { myClass<int,int> class1(3,5); class1.show(); myClass<int,char> class2(3,'a'); class2.show(); myClass<double,int> class3(2.9,10); class3.show(); system("PAUSE"); }
最后結果顯示:
一般來說,非類型模板參數可以是常整數(包括枚舉)或者指向外部鏈接對象的指針。
那么就是說,浮點數是不行的,指向內部鏈接對象的指針是不行的。
5.模板類中的成員函數定義返回值為類中的typedef類型時候注意
如果模板類中的成員要訪問類中的typedef類型必須加上關鍵字typename來指明它是一個類型。
如一下代碼中的那個成員函數size:
1 #include <iostream> 2 #include <string> 3 4 template<typename Type> 5 class List 6 { 7 public: 8 typedef unsigned size_type; 9 public: 10 List(); 11 void push_back(const Type &value); 12 void push_front(const Type &value); 13 Type& front() const; 14 Type& back() const; 15 size_type size() const; 16 ~List(); 17 private: 18 struct DataType 19 { 20 Type Value; 21 struct DataType *next; 22 }head; 23 struct DataType *end; 24 }; 25 26 template<typename Type> 27 List<Type>::List() 28 { 29 head.next = 0; 30 end = &head; 31 } 32 33 template<typename Type> 34 void List<Type>::push_back(const Type &value) 35 { 36 DataType *tmp = new DataType; 37 tmp->Value = value; 38 tmp->next = 0; 39 end->next = tmp; 40 end = tmp; 41 } 42 43 template<typename Type> 44 void List<Type>::push_front(const Type &value) 45 { 46 DataType *tmp = new DataType; 47 tmp->Value = value; 48 tmp->next = 0; 49 tmp->next = head.next; 50 head.next = tmp; 51 } 52 53 template<typename Type> 54 Type& List<Type>::front() const 55 { 56 return head.next->Value; 57 } 58 59 template<typename Type> 60 Type& List<Type>::back() const 61 { 62 return end->Value; 63 } 64 65 template<typename Type> 66 typename List<Type>::size_type List<Type>::size() const 67 { 68 size_type iSize = 0; 69 struct DataType *begin = &head; 70 71 while (begin->next) 72 { 73 ++iSize; 74 } 75 76 return iSize; 77 } 78 79 template<typename Type> 80 List<Type>::~List() 81 { 82 struct DataType *tmp = &head; 83 84 while (tmp = tmp->next) 85 { 86 head.next = tmp->next; 87 free(tmp); 88 tmp = &head; 89 } 90 } 91 92 int main() 93 { 94 List<int> list; 95 list.push_back(100); 96 list.push_front(200); 97 std::cout << list.front() << "/t" << list.back() << std::endl; 98 99 system("pause"); 100 return 0; 101 }