• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 195,  comments - 30,  trackbacks - 0

            在模板類中輸入流“》”和輸出流“《”的重載,若使用友元在類內聲明,在類外實現,那么連接時將會報錯,但我們可以采用以下三種方式來實現輸出流"<<"和"輸入流>>"的重載。

            一、將輸出流"<<"和"輸入流>>"重載的實現寫在類中

            #include "stdafx.h"
            #include   <iostream>
            using   namespace   std;  
               
            template<class T>  
            class Test  
            {  
               public:  
                  Test(const T& t):data(t){}
                  //---------------------------------------------
                  friend ostream& operator<<(ostream& out,Test<T>& t)    //輸出流重載聲明及實現
                  {
                       return out<<"data   is   "<<t.data;
                  } //--------------------------------------------
                  friend istream& operator>>(istream& in,Test<T>& t)      //輸入流重載聲明及實現
                  {
                      return in>>t.data;
                  }//---------------------------------------------
               private:  
                  T data;  
            };//-----------------------------------------------------------------      
                  
            int   main()  
            {  
               Test<int> b(3);  
               cout<<b<<'\n';  
               cin>>b;
               cout<<b<<'\n';
               return 0;
            }

            那么輸入輸出流重載為什么不能在類內聲明,類外實現呢??因為模板比較特殊,若果在模板類外實現重載的話:

            template<class T>
            ostream& operator<<(ostream& out,Test<T>& t)
            {
                    return out<<"data   is   "<<t.data;
            } //--------------------------------------------

            上面正好是函數模板的定義,而我們知道操作符重載函數不是類的成員函數,因此此處相當于定義了一個新的函數模板(不同于類中的friend ostream& operator<<(ostream& out,Test<T>& t) )。但若去掉template<class T> ,函數中的參數Test<T>就不知是什么類型,所以不能在模板類內聲明,類外實現操作符重載。

            二、既然類外實現相當于重定義了一個函數模板,那么只要他不使用類的私用成員即可,因此重載的函數模板只有通過類的公有成員函數來實現對類的私有成員的操作,這樣不必在類內聲明它為友元,直接在類外重載即可。

            #include "stdafx.h"
            #include   <iostream>
            using   namespace   std;  
               
            template<class T>  
            class Test  
            {  
               public:  
                  Test(const T& t):data(t){}
                  T GetData()const{return data;}
                  void SetData(T &item){data=item;}
               private:  
                  T data;  
            };//-----------------------------------------------------------------
            template<class T>      
            ostream& operator<<(ostream& out,Test<T>& t)   
            {
                  return out<<"data   is   "<<t.GetData();
            } //--------------------------------------------
            template<class T>
            istream& operator>>(istream& in,Test<T>& t)     
            {
                 T item;
                in>>item;
                t.SetData(item);
                return in;
            }//---------------------------------------------      
            int   main()  
            {  
               Test<int> b(3);  
               cout<<b<<'\n';  
               cin>>b;
               cout<<b<<'\n';
               return 0;
            }  

            三、使用過渡函數

            #include "stdafx.h"
            #include   <iostream>
            using   namespace   std;  
               
            template<class   T>  
            class   Test  
            {  
               public:  
                  Test(const T& t):data(t){}
                  //---------------------------------------------
                  template<class CharT,class CharTraits>  
                  basic_ostream<CharT,CharTraits>& Output(basic_ostream<CharT,CharTraits>& out)const    //輸出流過渡函數
                  {
                        return out<<"data   is   "<<data;
                  } //--------------------------------------------
                  template<class   CharT,class   CharTraits>  
                  basic_istream<CharT,CharTraits>& Input(basic_istream<CharT,CharTraits>& in)    //輸入流過渡函數
                  {
                      return in>>data;
                  }//---------------------------------------------
               private:  
                  T data;  
            };//-----------------------------------------------------------------      
            template<class T,class CharT,class CharTraits>  
            basic_ostream<CharT,CharTraits>& operator<<(basic_ostream<CharT,CharTraits>& out,const Test<T>& t)   //輸出流重載
            {  
                return t.Output(out);  
            }//------------------------------------------------------------------      
            template<class T,class CharT,class CharTraits>  
            basic_istream<CharT,CharTraits>& operator>>(basic_istream<CharT,CharTraits>& in,Test<T>& t)   //輸入流重載
            {  
                return t.Input(in);  
            }//------------------------------------------------------------------      
            int   main()  
            {  
               Test<int>   b(4);  
               cout<<b<<'\n';  
               cin>>b;
               cout<<b<<'\n';
               return 0;
            }

            posted on 2011-02-15 01:04 luis 閱讀(552) 評論(0)  編輯 收藏 引用
            <2009年7月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            友情鏈接

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲中文字幕无码一久久区| 久久久久国产精品| 欧美日韩精品久久久免费观看| 噜噜噜色噜噜噜久久| 久久精品国产亚洲AV麻豆网站 | 国产亚州精品女人久久久久久 | 国产精品乱码久久久久久软件| 亚洲综合日韩久久成人AV| 欧美亚洲另类久久综合| 久久只有这里有精品4| 国产亚洲欧美成人久久片| 欧美国产精品久久高清| 久久久久女人精品毛片| 麻豆久久| 久久久久国产日韩精品网站| 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 久久99国产综合精品| 久久91精品国产91| 伊人久久大香线焦综合四虎| 狠狠色丁香婷婷久久综合| 97超级碰碰碰碰久久久久| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 精品久久久久久久久免费影院| 97精品国产91久久久久久| 国产精品久久新婚兰兰| 久久久久国产| 久久久久亚洲av成人无码电影| 久久国产精品一区二区| 精品免费久久久久久久| 久久综合国产乱子伦精品免费| 久久精品中文无码资源站| 久久久久久国产精品美女 | 国产综合久久久久久鬼色| 99久久香蕉国产线看观香| 看全色黄大色大片免费久久久 | 国产亚洲婷婷香蕉久久精品| 久久综合给久久狠狠97色 | 亚洲国产精品久久久天堂| 久久久久亚洲AV片无码下载蜜桃| 最新久久免费视频| 青青草原综合久久大伊人|