模板類中操作符重載問題_第1頁(yè)
模板類中操作符重載問題_第2頁(yè)
模板類中操作符重載問題_第3頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、模板類中操作符重載問題("<<"和">>"重載) c+在模板類中輸入流“ ” 和輸出流 “ ” 的重載 ,若使用友元在類內(nèi)聲明,在類外實(shí)現(xiàn),那么連接時(shí)將會(huì)報(bào)錯(cuò) ,但我們可以采用以下三種方式來實(shí)現(xiàn)輸出流 "<<" 和 "輸入流 >>" 的重載。一、將輸出流 "<&l

2、t;" 和 "輸入流 >>" 重載的實(shí)現(xiàn)寫在類中#include "stdafx.h"#include<iostream>usingnamespacestd;template<class T>class Testpublic:Test(const T& t):data(t)/-friend ostream& operator<<(ostream&out,Test<T&

3、;gt;& t)/輸出流重載聲明及實(shí)現(xiàn)return out<<"datais"<<t.data; /-friend istream& operator>>(istream&in,Test<T>& t)/輸入流重載聲明及實(shí)現(xiàn)return in>>t.data;/-private:T data;/-intmain()Test<int> b(3);co

4、ut<<b<<'n'cin>>b;cout<<b<<'n'return 0;那么輸入輸出流重載為什么不能在類內(nèi)聲明,類外實(shí)現(xiàn)呢?因?yàn)槟0灞容^特殊,若果在模板類外實(shí)現(xiàn)重載的話:template<class T>ostream& operator<<(ostream& out,Test<T>& t)

5、 return out<<"datais"<<t.data; /-上面正好是函數(shù)模板的定義,而我們知道操作符重載函數(shù)不是類的成員函數(shù),因此此處相當(dāng)于定義了一個(gè)新的函數(shù)模板(不同于類中的friend ostream&operator<<(ostream& out,Test<T>& t))。但若去掉template<class T>,函數(shù)中的參數(shù)Test<T> 就不

6、知是什么類型, 所以不能在模板類內(nèi)聲明,類外實(shí)現(xiàn)操作符重載。二、既然類外實(shí)現(xiàn)相當(dāng)于重定義了一個(gè)函數(shù)模板,那么只要他不使用類的私用成員即可,因此重載的函數(shù)模板只有通過類的公有成員函數(shù)來實(shí)現(xiàn)對(duì)類的私有成員的操作,這樣不必在類內(nèi)聲明它為友元,直接在類外重載即可。#include "stdafx.h"#include<iostream>usingnamespacestd;template<class T>class Testpublic:Test(const T& t):data(t)T GetData()co

7、nstreturn 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&a

8、mp;gt;istream& operator>>(istream& in,Test<T>& t) T item;in>>item;t.SetData(item); return in;/-intmain()Test<int> b(3);cout<<b<<'n'cin>>b;cout<<b<&am

9、p;lt;'n'return 0;三、使用過渡函數(shù)#include "stdafx.h"#include<iostream>usingnamespacestd;template<classT>classTestpublic:Test(const T& t):data(t)/-template<class CharT,class CharTraits> basic_ostream<CharT,CharTraits>&O

10、utput(basic_ostream<CharT,CharTraits>&out)const/輸出流過渡函數(shù)return out<<"datais"<<data; /-template<classCharT,classCharTraits>basic_istream<CharT,CharTraits>& Input(basic_istream<CharT,CharTraits&gt

11、;& in)/輸入流過渡函數(shù)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)/輸

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論