类模板

来源:互联网 发布:微信企业号源码 编辑:程序博客网 时间:2024/05/17 22:04

这一小节专门介绍类模板,先看一个简单的例子:

[cpp] view plaincopy
  1. template <class Type> class Test  
  2. {  
  3. public:  
  4.   
  5.     Test(Type val):value(val){}  
  6.     void set(const Type &val){value = val;}  
  7.     Type get(){return value;}  
  8.   
  9. private:  
  10.     Type value;  
  11.   
  12. };  
  13.   
  14. int main()  
  15. {  
  16.     Test<int> iTest(5);  
  17.     cout<<iTest.get()<<endl;  
  18.   
  19.     Test<string> sTest("abc");  
  20.     sTest.set("aaa");  
  21.     cout<<sTest.get()<<endl;  
  22.     return 0;  
  23. }  


 

看起来也不是很复杂,它的定义也是以template后接模板形参表。在我们使用时,只需要指定模板实参就行了。


下面我们看一个稍微复杂一点的例子,使用stl标准库中的list来实现队列:

[cpp] view plaincopy
  1. #ifndef QUEUE_H  
  2. #define QUEUE_H  
  3.   
  4.   
  5. #include <iostream>  
  6. #include <list>  
  7.   
  8. using namespace std;  
  9.   
  10. template <class Type> class Queue;  
  11. //重载输出操作符  
  12. template <class Type>  
  13. std::ostream& operator<<(std::ostream&,const Queue<Type>&);  
  14.   
  15. template <class Type> class Queue  
  16. {  
  17.     friend std::ostream& operator<< <Type>(std::ostream&,const Queue<Type>&);  
  18. public:  
  19.     //构造函数:空函数,编译器会调用list的构造函数来初始化数据成员  
  20.     Queue() {}  
  21.     //接受一对迭代器来初始化:表用list的初始化函数  
  22.     template <class It> Queue(It beg,It end):items(beg,end){}  
  23.     template <class Iter> void assign(Iter beg,Iter end)  
  24.     {  
  25.         items.assign(beg,end);  
  26.     }  
  27.   
  28.     //返回队列的第一个元素  
  29.     Type& front()  
  30.     {  
  31.         return items.front();  
  32.     }  
  33.   
  34.     const Type &front()const  
  35.     {  
  36.         return items.front();  
  37.     }  
  38.   
  39.     void push(const Type &t)  
  40.     {  
  41.         items.push_back(t);  
  42.     }  
  43.   
  44.     void pop()  
  45.     {  
  46.         items.erase(items.begin());  
  47.     }  
  48.   
  49.     bool empty()const  
  50.     {  
  51.         return items.empty();  
  52.     }  
  53. private:  
  54.     std::list<Type> items;  
  55. };  
  56.   
  57. template <class Type>  
  58. std::ostream& operator<<(std::ostream &os,const Queue<Type> &q)  
  59. {  
  60.     os<<"<";     
  61.     std::list<Type>::const_iterator beg = q.items.begin();  
  62.     while(beg != q.items.end())  
  63.     {  
  64.         os<<*beg<<" ";  
  65.             ++beg;  
  66.     }  
  67.     os<<">";  
  68.     return os;  
  69. }  
  70.   
  71. #endif  


这个设计的风格与标准库中的容器适配器类似,只不过标准库中是使用deque实现的。

最后给出一个比较复杂的例子:用类模板实现队列。不同于上面的设计,这个设计则并没有使用stl库,而是实实在在的动了底层的数据结构。
其实它的设计也不是很复杂,只用到了两个类:第一个类储存的是具体的元素和指向下个元素的指针,第二类储存的是队列的头部和尾部,并为这个队列设计了一些接口:

 

[cpp] view plaincopy
  1. #ifndef QUEUE_H  
  2. #define QUEUE_H  
  3.   
  4. #include <iostream>  
  5. //注意两个声明的顺序  
  6.   
  7. //Queue模板类的声明:因为Queue的特例(不是全部实例)是QueueItem的友元  
  8. template <class Type> class Queue;  
  9. //模板函数声明:因为要使用它的特例作为友元类  
  10. template <class T> std::ostream& operator<<(std::ostream&, const Queue<T>&);  
  11. //模板函数声明  
  12. template <class T> std::istream& operator>>(std::istream&,  Queue<T>&);  
  13.   
  14.   
  15. template <class Type> class QueueItem  
  16. {  
  17.     //设为友元类,是得Queue能访问自己的成员  
  18.     friend Queue<Type>;  
  19.     //  
  20.     friend std::ostream& operator<< <Type> (std::ostream&, const Queue<Type>&);   
  21.     friend std::istream& operator>> <Type> (std::istream&, Queue<Type>&);  
  22.     //构造函数:用传来的值初始化自己的数据,指针为空  
  23.     QueueItem(const Type &t):item(t),next(0){}  
  24.     //储存元素的值  
  25.     Type item;  
  26.     //指向下一个元素  
  27.     QueueItem *next;  
  28. };  
  29.   
  30.   
  31. template<class Type>class Queue  
  32. {  
  33.     friend std::ostream& operator<< <Type> (std::ostream&, const Queue<Type>&);  
  34.     friend std::istream& operator>> <Type> (std::istream&, Queue<Type>&);  
  35. public:  
  36.     //默认构造函数:空队列  
  37.     Queue():head(0),tail(0){}  
  38.   
  39.     //复制构造函数:调用copy_elems函数复制队列  
  40.     Queue(const Queue &Q):head(0),tail(0){copy_elems(Q);}  
  41.   
  42.     //类成员函数模板定义:  
  43.     //接受一对迭代器的构造函数  
  44.     //注意,这里没有使用引用  
  45.     //当实参是数组时,传进来的是指针  
  46.     template <class It>Queue(It beg, It end):head(0),tail(0)  
  47.     {  
  48.         copy_elems(beg,end);  
  49.     }  
  50.   
  51.     //类成员函数模板声明  
  52.     //接受一对迭代器,将其间的值传递给队列  
  53.     template <class Iter> void assign(Iter,Iter);  
  54.   
  55.     //重载赋值操作符  
  56.     Queue& operator=(const Queue&);  
  57.   
  58.     //析构函数:效用detory函数完成  
  59.     ~Queue(){destroy();}  
  60.   
  61.     //返回队列的头部元素,两个版本  
  62.     Type& front(){return head->item;}  
  63.   
  64.     const Type &front()const {return head->item;}  
  65.   
  66.     //给队尾增加一个元素  
  67.     void push(const Type &);  
  68.   
  69.     //删除队列头部的元素  
  70.     void pop();  
  71.   
  72.     //判断队列是否为空  
  73.     bool empty()const {return head == 0;}  
  74. private:  
  75.     //指向队首  
  76.     QueueItem<Type> *head;  
  77.     //指向队尾  
  78.     QueueItem<Type> *tail;  
  79.     void destroy();  
  80.     void copy_elems(const Queue&);  
  81.     template <class  Iter> void copy_elems(Iter,Iter);  
  82. };  
  83.   
  84. #include "queue.cpp"  
  85.   
  86. #endif  


下面是部分接口的实现:

 

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include "queue.h"  
  3.   
  4. using std::ostream;  
  5. using std::istream;  
  6.   
  7. template <class Type> void Queue<Type>::destroy()  
  8. {  
  9.     while(!empty())  
  10.         pop();  
  11. }  
  12.   
  13. template <class Type> void Queue<Type>::pop()  
  14. {  
  15.     QueueItem<Type>* p = head;  
  16.     head = head->next;  
  17.     delete p;  
  18. }  
  19.   
  20. template <class Type> void Queue<Type>::push(const Type &val)  
  21. {  
  22.     //分配一个新的QueueItem对象,并用传递的值初始化它  
  23.     QueueItem<Type>* pt = new QueueItem<Type>(val);  
  24.     //如果是一个空队列:那么队首与队尾均指向这个元素  
  25.     if(empty())  
  26.         head = tail = pt;  
  27.     //如果队列已经有元素了,那么将它放在队尾  
  28.     else  
  29.     {  
  30.         tail->next = pt;  
  31.         tail = pt;  
  32.     }  
  33. }  
  34.   
  35. template <class Type> void Queue<Type>::copy_elems(const Queue &orig)  
  36. {  
  37.     //最后一个指针本身就为0  
  38.     for(QueueItem<Type>* pt = orig.head;pt;pt = pt->next)  
  39.         push(pt->item);  
  40. }  
  41.   
  42. template <class Type> Queue<Type>& Queue<Type>::operator=(const Queue& orig)  
  43. {  
  44.     //删除原来的队列  
  45.     detroy();  
  46.     //复制新的队列  
  47.     copy_elems(orig);  
  48.     return *this;  
  49. }  
  50.   
  51. template <class Type>  
  52. ostream& operator<<(ostream &os, const Queue<Type> &q)  
  53. {  
  54.     os << "< ";  
  55.     QueueItem<Type> *p;  
  56.     for (p = q.head; p; p = p->next)  
  57.             os << p->item << " ";  
  58.     os <<">";  
  59.     return os;  
  60. }  
  61.   
  62. template<class Type>   
  63. istream& operator>> (istream &is,Queue<Type> &q)  
  64. {  
  65.     Type val;  
  66.     while(is>>val)  
  67.         q.push(val);  
  68.     return is;  
  69. }  
  70.   
  71. //类外定义成员函数模板:  
  72. //两个形参列表:类模板形参,自己模板的形参  
  73. template <class T> template <class Iter>  
  74. void Queue<T>::assign(Iter beg,Iter end)  
  75. {  
  76.     destroy();  
  77.     copy_elems(beg,end);  
  78. }  
  79.   
  80. //类外定义成员函数模板:  
  81. //两个形参列表:类模板形参,自己模板的形参  
  82. template <class Type> template <class It>  
  83. void Queue<Type>::copy_elems(It beg,It end)  
  84. {  
  85.     while(beg != end)  
  86.     {  
  87.         push(*beg);  
  88.         ++beg;  
  89.     }  
  90. }  


 

程序里值得说道的地方很多,我们慢慢缕一缕:
首先,在定义友元之前,我们对友元进行了声明。这一点跟我们之前学的不一样。以前为一个类定义友元的时候,是不需要声明,friend关键字会自动扩展作用域。但是这里我们却必须对其声明,原因在于,我们定义的友元,也是模板,如果不声明,会是得友元函数和友元类的所有实例都能访问类的私有成员,这并不是我们所希望的。当我们的QueueItem的内容是int型时,让Queue<string>类型也能访问它是没有必要,也是不安全的,所以这里的定义是得只有与QueueItem同类型的Queue类以及输入输出操作才能访问它。

其次类中声明的友元函数:输入输出操作符friend std::ostream& operator<< <Type> (std::ostream&, const Queue<Type>&);这里为什么要加<Type>?因为如果不加,编译器会将这个函数是为非模板函数,而对后面的定义视而不见。去掉<Type>以后,编译就会显示不能访问QueueItem的私有成员,因为主函数中调用的cout<<  qi;用的是模板函数,而我们并没有把它声明为友元,所以就提示不能访问私有成员了。

一般情况下,使用类模板名字的时候,都得指定类模板形参,但是有一种情况是例外:当你在类的作用域内部的时候就不需要了。比如构造函数并没有写成:QueueItem<Type>(const Type &t):item(t),next(0){},Queue& operator=(const Queue&);这里的返回值也没有加上<Type>。但是,如果你使用的并不是自己类的类型,而是其他类,就必须加上了:比如Queue中私有成员的那两个指向QueueItem的指针。

类模板中的成员函数也是模板,只有在使用时才会被实例化。定义模板类型的对象时,模板被实例化,并且会实例化用于初始该对象的构造函数以及构造函数调用的其它函数。当你使用Queue<int> qi(a,a+4);初始化一个队列时,构造函数:template <class It>Queue(It beg, It end)以及它所调用的template <class Iter> void copy_elems(Iter,Iter);就会被实例化;当你调用qi.push(m);时,void push(const Type &);才会被实例化。


与普通函数模板不同的是,由于类实例化时已经确定了成员函数的类型,所以这里不需要再进行模板实参推断了。这意味着,当调用函数的实参与模板实例化后的形参不完全匹配时,可以会发生常规类型转换,而不像函数模板,如果不匹配,就一棒子打死的情况。举一个例子,对于Queue<int> qi,你仍然可以short m = 4;qi.push(m);

还有一个之前没有见过的地方就是我们的类模板中,有一些成员或者函数本身就是模板。比如Queue中的接受一对迭代器的构造函数和assign函数。(标准的queue中并没有assign函数),如果它们在类外定义,就需要注意了:它们包含两个模板形参表,其中第一个是类模板形参,第二个才是自己的模板形参。

最后不得不提一下整个类的设计,虽然这个类提供了很多接口,但它们之间并不是相互独立的。单独定义的,只有pop,push和front三个操作。而其中pop和push操作就是最底层也是最基本的操作。其他触及元素的操作,都依赖它们两个实现。这样的设计非常简洁。但是一开始却不一定能想得到。

 

最后提一句题外话,由于编译模型的缘故,我们并没有把对应的.cpp加入工程。因为.h中包含了.cpp而.cpp中又包含了.h。如果把.cpp加入工程那么它的内容会被编译两次,自然就会出现重定义了。

原创粉丝点击