类模板与函数模板的定义和使用类似。

有时,有两个或多个类,其功能是相同的,仅仅是数据类型不同,如下面语句声明了一个类:

 

1. ​​class Compare_int​​
2. ​​{​​
3. ​​public :​​
4. ​​Compare(int a,int b)​​
5. ​​{​​
6. ​​x=a;​​
7. ​​y=b;​​
8. ​​}​​
9. ​​int max( )​​
10. ​​{​​
11. ​​return (x>y)?x:y;​​
12. ​​}​​
13. ​​int min( )​​
14. ​​{​​
15. ​​return (x<y)?x:y;​​
16. ​​}​​
17. ​​private :​​
18. ​​int x,y;​​
19. ​​};​​

​其作用是对两个整数作比较,可以通过调用成员函数max和min得到两个整数中的大者和小者。

如果想对两个浮点数(float型)作比较,需要另外声明一个类:​

 

1. class Compare_float
2. {

3. public :
4. Compare(float a,float b)
5. {

6. x=a;y=b;
7. }
8. float max( )
9. {

10. return (x>y)?x:y;
11. }
12. float min( )
13. {

14. return (x<y)?x:y;
15. }
16. private :
17. float x,y;
18. }

显然这基本上是重复性的工作,应该有办法减少重复的工作。

C++在发展的后期增加了模板(template )的功能,提供了解决这类问题的途径。可以声明一个通用的类模板,它可以有一个或多个虚拟的类型参数,如对以上两个类可以综合写出以下的类模板:

 


1. ​​template <class numtype> //声明一个模板,虚拟类型名为numtype​​
2. ​​class Compare //类模板名为Compare​​
3. ​​{​​
4. ​​public :​​
5. ​​Compare(numtype a,numtype b)​​
6. ​​{​​
7. ​​x=a;y=b;​​
8. ​​}​​
9. ​​numtype max( )​​
10. ​​{​​
11. ​​return (x>y)?x:y;​​
12. ​​}​​
13. ​​numtype min( )​​
14. ​​{​​
15. ​​return (x<y)?x:y;​​
16. ​​}​​
17. ​​private :​​
18. ​​numtype x,y;​​
19. ​​};​​

​请将此类模板和前面第一个Compare_int类作一比较,可以看到有两处不同。

1) 声明类模板时要增加一行
    template <class 类型参数名>
template意思是“模板”,是声明类模板时必须写的关键字。在template后面的尖括号内的内容为模板的参数表列,关键字class表示其后面的是类型参数。在本例中numtype就是一个类型参数名。这个名宇是可以任意取的,只要是合法的标识符即可。这里取numtype只是表示“数据类型”的意思而已。此时,mimtype并不是一个已存在的实际类型名,它只是一个虚拟类型参数名。在以后将被一个实际的类型名取代。

2) 原有的类型名int换成虚拟类型参数名numtype。
在建立类对象时,如果将实际类型指定为int型,编译系统就会用int取代所有的numtype,如果指定为float型,就用float取代所有的numtype。这样就能实现“一类多用”。

由于类模板包含类型参数,因此又称为参数化的类。如果说类是对象的抽象,对象是类的实例,则类模板是类的抽象,类是类模板的实例。利用类模板可以建立含各种数据类型的类。

那么,在声明了一个类模板后,怎样使用它呢?怎样使它变成一个实际的类?

先回顾一下用类来定义对象的方法:
    Compare_int cmp1(4,7); // Compare_int是已声明的类
其作用是建立一个Compare_int类的对象,并将实参4和7分别赋给形参a和b,作为进 行比较的两个整数。

用类模板定义对象的方法与此相似,但是不能直接写成
   Compare cmp(4,7); // Compare是类模板名
Compare是类模板名,而不是一个具体的类,类模板体中的类型numtype并不是一个实际的类型,只是一个虚拟的类型,无法用它去定义对象。必须用实际类型名去取代虚拟的类型,具体的做法是:
    Compare <int> cmp(4,7);
即在类模板名之后在尖括号内指定实际的类型名,在进行编译时,编译系统就用int取代类模板中的类型参数numtype,这样就把类模板具体化了,或者说实例化了。这时Compare<int>就相当于前面介绍的Compare_int类。

[例9.14] 声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。​

 

1. ​​#include <iostream>​​
2. ​​using namespace std;​​
3. ​​template <class numtype>​​
4. ​​//定义类模板​​
5. ​​class Compare​​
6. ​​{​​
7. ​​public :​​
8. ​​Compare(numtype a,numtype b)​​
9. ​​{x=a;y=b;}​​
10. ​​numtype max( )​​
11. ​​{return (x>y)?x:y;}​​
12. ​​numtype min( )​​
13. ​​{return (x<y)?x:y;}​​
14. ​​private :​​
15. ​​numtype x,y;​​
16. ​​};​​
17. ​​int main( )​​
18. ​​{​​
19. ​​Compare<int > cmp1(3,7); //定义对象cmp1,用于两个整数的比较​​
20. ​​cout<<cmp1.max( )<<" is the Maximum of two integer numbers."<<endl;​​
21. ​​cout<<cmp1.min( )<<" is the Minimum of two integer numbers."<<endl<<endl;​​
22. ​​Compare<float > cmp2(45.78,93.6); //定义对象cmp2,用于两个浮点数的比较​​
23. ​​cout<<cmp2.max( )<<" is the Maximum of two float numbers."<<endl;​​
24. ​​cout<<cmp2.min( )<<" is the Minimum of two float numbers."<<endl<<endl;​​
25. ​​Compare<char> cmp3(′a′,′A′); //定义对象cmp3,用于两个字符的比较​​
26. ​​cout<<cmp3.max( )<<" is the Maximum of two characters."<<endl;​​
27. ​​cout<<cmp3.min( )<<" is the Minimum of two characters."<<endl;​​
28. ​​return 0;​​
29. ​​}​​

运行结果如下:
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.

还有一个问题要说明: 上面列出的类模板中的成员函数是在类模板内定义的。如果改为在类模板外定义,不能用一般定义类成员函数的形式:
    numtype Compare::max( ) {…} //不能这样定义类模板中的成员函数
而应当写成类模板的形式:
    template <class numtype>
    numtype Compare<numtype>::max( )
    {
        return (x>y)?x:y;
    }
上面第一行表示是类模板,第二行左端的numtype是虚拟类型名,后面的Compare <numtype>是一个整体,是带参的类。表示所定义的max函数是在类Compare <numtype>的作用域内的。在定义对象时,用户当然要指定实际的类型(如int),进行编译时就会将类模板中的虚拟类型名numtype全部用实际的类型代替。这样Compare <numtype >就相当于一个实际的类。大家可以将例9.14改写为在类模板外定义各成员 函数。

归纳以上的介绍,可以这样声明和使用类模板:
1) 先写出一个实际的类。由于其语义明确,含义清楚,一般不会出错。

2) 将此类中准备改变的类型名(如int要改变为float或char)改用一个自己指定的虚拟类型名(如上例中的numtype)。

3) 在类声明前面加入一行,格式为:
    template <class 虚拟类型参数>
如:
    template <class numtype> //注意本行末尾无分号
    class Compare
    {…}; //类体

4) 用类模板定义对象时用以下形式:
    类模板名<实际类型名> 对象名;
    类模板名<实际类型名> 对象名(实参表列);
如:
    Compare<int> cmp;
    Compare<int> cmp(3,7);

5) 如果在类模板外定义成员函数,应写成类模板形式:
   template <class 虚拟类型参数>
   函数类型 类模板名<虚拟类型参数>::成员函数名(函数形参表列) {…}

关于类模板的几点说明:
1) 类模板的类型参数可以有一个或多个,每个类型前面都必须加class,如:
    template <class T1,class T2>
    class someclass
    {…};
在定义对象时分别代入实际的类型名,如:
    someclass<int,double> obj;

2) 和使用类一样,使用类模板时要注意其作用域,只能在其有效作用域内用它定义对象。

3) 模板可以有层次,一个类模板可以作为基类,派生出派生模板类。有关这方面的知识实际应用较少,本教程暂不作介绍,感兴趣的同学可以自行学习。