模板类中操作符重载问题

来源:互联网 发布:南阳企业网站seo 编辑:程序博客网 时间:2024/05/01 17:39
转自:没有找到原文

在模板类中输入流“>>”和输出流“<<”的重载,若使用友元在类内声明,在类外实现,那么连接时将会报错,但我们可以采用以下三种方式来实现输出流"<<"和"输入流>>"的重载。

一、将输出流"<<"和"输入流>>"重载的实现写在类中

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include   <iostream>  
  3. using   namespace   std;    
  4.      
  5. template<class T>    
  6. class Test    
  7. {    
  8.    public:    
  9.       Test(const T& t):data(t){}  
  10.       //---------------------------------------------  
  11.       friend ostream& operator<<(ostream& out,Test<T>& t)    //输出流重载声明及实现  
  12.       {  
  13.            return out<<"data   is   "<<t.data;  
  14.       } //--------------------------------------------  
  15.       friend istream& operator>>(istream& in,Test<T>& t)      //输入流重载声明及实现  
  16.       {  
  17.           return in>>t.data;  
  18.       }//---------------------------------------------  
  19.    private:    
  20.       T data;    
  21. };//-----------------------------------------------------------------        
  22.         
  23. int   main()    
  24. {    
  25.    Test<int> b(3);    
  26.    cout<<b<<'\n';    
  27.    cin>>b;  
  28.    cout<<b<<'\n';  
  29.    return 0;  
  30. }  

那么输入输出流重载为什么不能在类内声明,类外实现呢??因为模板比较特殊,若果在模板类外实现重载的话:

[cpp] view plaincopy
  1. template<class T>  
  2. ostream& operator<<(ostream& out,Test<T>& t)  
  3. {  
  4.         return out<<"data   is   "<<t.data;  
  5. //--------------------------------------------  

上面正好是函数模板的定义,而我们知道操作符重载函数不是类的成员函数,因此此处相当于定义了一个新的函数模板(不同于类中的friend ostream& operator<<(ostream& out,Test<T>& t) )。但若去掉template<class T> ,函数中的参数Test<T>就不知是什么类型,所以不能在模板类内声明,类外实现操作符重载。

二、既然类外实现相当于重定义了一个函数模板,那么只要他不使用类的私用成员即可,因此重载的函数模板只有通过类的公有成员函数来实现对类的私有成员的操作,这样不必在类内声明它为友元,直接在类外重载即可。

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include   <iostream>  
  3. using   namespace   std;    
  4.      
  5. template<class T>    
  6. class Test    
  7. {    
  8.    public:    
  9.       Test(const T& t):data(t){}  
  10.       T GetData()const{return data;}  
  11.       void SetData(T &item){data=item;}  
  12.    private:    
  13.       T data;    
  14. };//-----------------------------------------------------------------  
  15. template<class T>        
  16. ostream& operator<<(ostream& out,Test<T>& t)     
  17. {  
  18.       return out<<"data   is   "<<t.GetData();  
  19. //--------------------------------------------  
  20. template<class T>  
  21. istream& operator>>(istream& in,Test<T>& t)       
  22. {  
  23.      T item;  
  24.     in>>item;  
  25.     t.SetData(item);  
  26.     return in;  
  27. }//---------------------------------------------        
  28. int   main()    
  29. {    
  30.    Test<int> b(3);    
  31.    cout<<b<<'\n';    
  32.    cin>>b;  
  33.    cout<<b<<'\n';  
  34.    return 0;  
  35. }  

三、使用过渡函数

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include   <iostream>  
  3. using   namespace   std;    
  4.      
  5. template<class   T>    
  6. class   Test    
  7. {    
  8.    public:    
  9.       Test(const T& t):data(t){}  
  10.       //---------------------------------------------  
  11.       template<class CharT,class CharTraits>    
  12.       basic_ostream<CharT,CharTraits>& Output(basic_ostream<CharT,CharTraits>& out)const    //输出流过渡函数  
  13.       {  
  14.             return out<<"data   is   "<<data;  
  15.       } //--------------------------------------------  
  16.       template<class   CharT,class   CharTraits>    
  17.       basic_istream<CharT,CharTraits>& Input(basic_istream<CharT,CharTraits>& in)    //输入流过渡函数  
  18.       {  
  19.           return in>>data;  
  20.       }//---------------------------------------------  
  21.    private:    
  22.       T data;    
  23. };//-----------------------------------------------------------------        
  24. template<class T,class CharT,class CharTraits>    
  25. basic_ostream<CharT,CharTraits>& operator<<(basic_ostream<CharT,CharTraits>& out,const Test<T>& t)   //输出流重载  
  26. {    
  27.     return t.Output(out);    
  28. }//------------------------------------------------------------------        
  29. template<class T,class CharT,class CharTraits>    
  30. basic_istream<CharT,CharTraits>& operator>>(basic_istream<CharT,CharTraits>& in,Test<T>& t)   //输入流重载  
  31. {    
  32.     return t.Input(in);    
  33. }//------------------------------------------------------------------        
  34. int   main()    
  35. {    
  36.    Test<int>   b(4);    
  37.    cout<<b<<'\n';    
  38.    cin>>b;  
  39.    cout<<b<<'\n';  
  40.    return 0;  
  41. }  
0 0
原创粉丝点击