c++类模板

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

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;
};

其作用是对两个整数作比较,可以通过调用成员函数max和min得到两个整数中的大者和小者。如果想对两个浮点数(float型)作比较,需要另外声明一个类:

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;
}

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

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

template <class numtype> //声明一个模板,虚拟类型名为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;
};

请将此类模板和前面第一个Compare_int类作一比较,可以看到有两处不同:
1)声明类模板时要增加一行
template < class 类型参数名>
2)原有的类型名int换成虚拟类型参数名numtype。

在建立类对象时,如果将实际类型指定为int型,编译系统就会用int取代所有的numtype,如果指定为float型,就用float取代所有的numtype。这样就能实现“一类多用”。由于类模板包含类型参数,因此又称为参数化的类。如果说类是对象的抽象,对象是类的实例,则类模板是类的抽象,类是类模板的实例。利用类模板可以建立含各种数据类型的类。在声明了一个类模板后,怎样使用它?怎样使它变成一个实际的类?

先回顾一下用类来定义对象的方法:

Compare_int cmp1(4,7); // Compare_int是已声明的类

用类模板定义对象的方法与此相似,但是不能直接写成

Compare cmp(4,7); // Compare是类模板名

Compare是类模板名,而不是一个具体的类,类模板体中的类型numtype并不是一个实际的类型,只是一个虚拟的类型,无法用它去定义对象。

必须用实际类型名去取代虚拟的类型,具体的做法是:

Compare cmp(4,7);

即在类模板名之后在尖括号内指定实际的类型名,在进行编译时,编译系统就用int取代类模板中的类型参数numtype,这样就把类模板具体化了,或者说实例化了。这时Compare就相当于前面介绍的Compare_int类。

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

#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);//定义对象cmp1,用于两个整数的比较
   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); //定义对象cmp2,用于两个浮点数的比较
   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′); //定义对象cmp3,用于两个字符的比较
   cout<<cmp3.max( )<<″ is the Maximum of two characters.″<<endl;
   cout<<cmp3.min( )<<″ is the Minimum of two characters.″<<endl;
   return 0;
}

结果

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;}

#include <iostream>
  using namespace 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()
  {
       Base <int>b(4) ;
       cout<<b.getA() ;
       Base <double> bc(4) ;
       bc.setA(4.3) ;
       cout<<bc.getA() ;
       system("pause") ;
   return 0 ;  
  }

总结

类模板的格式为:

    template< class 形参名,class 形参名,…> class 类名

    { … };

  类模板和函数模板都是以template开始后接模板形参列表组成,模板形参不能为空,一但声明了类模板就可以用类模板的形参名声明类中的成员变量和成员函数,即可以在类中使用内置类型的地方都可以使用模板形参名来声明。比如

    template< class T> class A{public: T a; T b; T hy(T c, T &d);};

在类A中声明了两个类型为T的成员变量a和b,还声明了一个返回类型为T带两个参数类型为T的函数hy。

  2、类模板对象的创建:比如一个模板类A,则使用类模板创建对象的方法为A m;在类A后面跟上一个<>尖括号并在里面填上相应的类型,这样的话类A中凡是用到模板形参的地方都会被int 所代替。当类模板有两个模板形参时创建对象的方法为A< int, double> m;类型之间用逗号隔开。

  3、对于类模板,模板形参的类型必须在类名后的尖括号中明确指定。比如A<2> m;用这种方法把模板形参设置为int是错误的(编译错误:error C2079: ‘a’ uses undefined class ‘A’),类模板形参不存在实参推演的问题。也就是说不能把整型值2推演为int 型传递给模板形参。要把类模板形参调置为int 型必须这样指定A m。

  4、在类模板外部定义成员函数的方法为:

    template<模板形参列表> 函数返回类型 类名<模板形参名>::函数名(参数列表){函数体},

比如有两个模板形参T1,T2的类A中含有一个void h()函数,则定义该函数的语法为:

 template< class T1,class T2> void A< T1,T2>::h(){}。

注意:当在类外面定义类的成员时template后面的模板形参应与要定义的类的模板形参一致。

  5、再次提醒注意:模板的声明或定义只能在全局,命名空间或类范围内进行。即不能在局部范围,函数内进行,比如不能在main函数中声明或定义一个模板。

猜你喜欢

转载自blog.csdn.net/qq_40828914/article/details/80643593