C++ 中的类模板
原文連接:http://see.xidian.edu.cn/cpp/biancheng/view/213.html
有時(shí),有兩個(gè)或多個(gè)類,其功能是相同的,僅僅是數(shù)據(jù)類型不同,如下面語(yǔ)句聲明了一個(gè)類:
class Compare_int
{
? ?public :
? ?Compare(int a,int b)
? ?{
? ? ? x=a;
? ? ? y=b;
? ?}
? ?int max( )
? ?{
? ? ? return (x>y)?x:y;
}
int min( )
{
? ?return (x<y)?x:y;}
? ?private :
? ?int x,y;
};
其作用是對(duì)兩個(gè)整數(shù)作比較,可以通過(guò)調(diào)用成員函數(shù)max和min得到兩個(gè)整數(shù)中的大者和小者。
如果想對(duì)兩個(gè)浮點(diǎn)數(shù)(float型)作比較,需要另外聲明一個(gè)類:
class Compare_float
{
? ?public :
? ?Compare(float a,float b)
? ?{x=a;y=b;}
? ?float max( )
? ?{return (x>y)?x:y;}
? ?float min( )
? ?{return (x<y)?x:y;}
? ?private :
? ?float x,y;
}
顯然這基本上是重復(fù)性的工作,應(yīng)該有辦法減少重復(fù)的工作。
C++在發(fā)展的后期增加了模板(template )的功能,提供了解決這類問(wèn)題的途徑。可以聲明一個(gè)通用的類模板,它可以有一個(gè)或多個(gè)虛擬的類型參數(shù),如對(duì)以上兩個(gè)類可以綜合寫出以下的類模板:
template <class numtype> //聲明一個(gè)模板,虛擬類型名為numtype
class Compare //類模板名為Compare
{
? ?public :
? ?Compare(numtype a,numtype b)
? ?{x=a;y=b;}
? ?numtype max( )
? ?{return (x>y)?x:y;}
? ?numtype min( )
? ?{return (x<y)?x:y;}
? ?private :
? ?numtype x,y;
};
請(qǐng)將此類模板和前面第一個(gè)Compare_int類作一比較,可以看到有兩處不同:
? ?template <class 類型參數(shù)名>
在建立類對(duì)象時(shí),如果將實(shí)際類型指定為int型,編譯系統(tǒng)就會(huì)用int取代所有的numtype,如果指定為float型,就用float取代所有的numtype。這樣就能實(shí)現(xiàn)“一類多用”。
由于類模板包含類型參數(shù),因此又稱為參數(shù)化的類。如果說(shuō)類是對(duì)象的抽象,對(duì)象是類的實(shí)例,則類模板是類的抽象,類是類模板的實(shí)例。
利用類模板可以建立含各種數(shù)據(jù)類型的類。在聲明了一個(gè)類模板后,怎樣使用它?怎樣使它變成一個(gè)實(shí)際的類?
先回顧一下用類來(lái)定義對(duì)象的方法:
? ?Compare_int cmp1(4,7); // Compare_int是已聲明的類
用類模板定義對(duì)象的方法與此相似,但是不能直接寫成
? ?Compare cmp(4,7); // Compare是類模板名
? ?Compare是類模板名,而不是一個(gè)具體的類,類模板體中的類型numtype并不是一個(gè)實(shí)際的類型,只是一個(gè)虛擬的類型,無(wú)法用它去定義對(duì)象。
必須用實(shí)際類型名去取代虛擬的類型,具體的做法是:
? ?Compare <int> cmp(4,7);
即在類模板名之后在尖括號(hào)內(nèi)指定實(shí)際的類型名,在進(jìn)行編譯時(shí),編譯系統(tǒng)就用int取代類模板中的類型參數(shù)numtype,這樣就把類模板具體化了,或者說(shuō)實(shí)例化了。這時(shí)Compare<int>就相當(dāng)于前面介紹的Compare_int類。
例9.14是一個(gè)完整的例子。
例9.14 聲明一個(gè)類模板,利用它分別實(shí)現(xiàn)兩個(gè)整數(shù)、浮點(diǎn)數(shù)和字符的比較,求出大數(shù)和小數(shù)。
#include <iostream>
using namespace std;
template <class numtype>
//定義類模板
class Compare
{
? ?public :
? ?Compare(numtype a,numtype b)
? ?{x=a;y=b;}
? ?numtype max( )
? ?{return (x>y)?x:y;}
? ?numtype min( )
? ?{return (x<y)?x:y;}
? ?private :
? ?numtype x,y;
};
int main( )
{
? ?Compare<int > cmp1(3,7);//定義對(duì)象cmp1,用于兩個(gè)整數(shù)的比較
? ?cout<<cmp1.max( )<<″ is the Maximum of two integer numbers.″<<endl;
? ?cout<<cmp1.min( )<<″ is the Minimum of two integer numbers.″<<endl<<endl;
? ?Compare<float > cmp2(45.78,93.6); //定義對(duì)象cmp2,用于兩個(gè)浮點(diǎn)數(shù)的比較
? ?cout<<cmp2.max( )<<″ is the Maximum of two float numbers.″<<endl;
? ?cout<<cmp2.min( )<<″ is the Minimum of two float numbers.″<<endl<<endl;
? ?Compare<char> cmp3(′a′,′A′); //定義對(duì)象cmp3,用于兩個(gè)字符的比較
? ?cout<<cmp3.max( )<<″ is the Maximum of two characters.″<<endl;
? ?cout<<cmp3.min( )<<″ is the Minimum of two characters.″<<endl;
? ?return 0;
}
運(yùn)行結(jié)果如下:
7 is the Maximum of two integers.
3 is the Minimum of two integers.
93.6 is the Maximum of two float numbers.
45.78 is the Minimum of two float numbers.
a is the Maximum of two characters.
A is the Minimum of two characters.
還有一個(gè)問(wèn)題要說(shuō)明: 上面列出的類模板中的成員函數(shù)是在類模板內(nèi)定義的。如果改為在類模板外定義,不能用一般定義類成員函數(shù)的形式:
? ?numtype Compare::max( ) {…} //不能這樣定義類模板中的成員函數(shù)
而應(yīng)當(dāng)寫成類模板的形式:
? ?template <class numtype>
? ?numtype Compare<numtype>::max( )
? ?{{return (x>y)?x:y;}
歸納以上的介紹,可以這樣聲明和使用類模板:
template <class 虛擬類型參數(shù)>,如
template <class numtype> //注意本行末尾無(wú)分號(hào)
class Compare
{…}; //類體
? ?類模板名<實(shí)際類型名> 對(duì)象名;
? ?類模板名<實(shí)際類型名> 對(duì)象名(實(shí)參表列);
如
? ?Compare<int> cmp;
? ?Compare<int> cmp(3,7);
? ?template <class 虛擬類型參數(shù)>
? ?函數(shù)類型 類模板名<虛擬類型參數(shù)>::成員函數(shù)名(函數(shù)形參表列) {…}
關(guān)于類模板的幾點(diǎn)說(shuō)明:
template <class T1,class T2>
class someclass
{…};
在定義對(duì)象時(shí)分別代入實(shí)際的類型名,如
? ?someclass<int,double> obj;
總結(jié)
- 上一篇: 关于工具类应用产品界面设计的一点思考
- 下一篇: 20140710文安c++面试总结