


版權(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>
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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 公章合同范本模板
- ui設(shè)計(jì)兼職合同范本
- 上游電子銷售合同范本
- 住宅抵押合同范本
- 借貸咨詢合同范本
- 農(nóng)村房車銷售合同范本
- 農(nóng)用器材采購(gòu)合同范本
- 中美二十天然氣合同范例
- 個(gè)人售賣二手車合同范本
- 出納公司合同范本
- 新能源汽車驅(qū)動(dòng)電機(jī)及控制系統(tǒng)檢修課件 學(xué)習(xí)情境1:驅(qū)動(dòng)電機(jī)的認(rèn)知
- 2024年采購(gòu)部年終總結(jié)
- 人教版(PEP)五年級(jí)英語下冊(cè)第一單元測(cè)試卷-Unit 1 My day 含答案
- 打深水井施工方案
- 企業(yè)名稱預(yù)先核準(zhǔn)通知書
- 統(tǒng)籌管理方案
- 建筑工程安全文明施工標(biāo)準(zhǔn)化圖集(附圖豐富)
- Unit 1 Travel教案-2023-2024學(xué)年高一下學(xué)期 中職英語高教版(2023修訂版)基礎(chǔ)模塊2
- DB3206T 1083-2024機(jī)關(guān)會(huì)議服務(wù)人員操作技術(shù)規(guī)范
- 習(xí)作《我的家人 》教案-2024-2025學(xué)年統(tǒng)編版語文四年級(jí)上冊(cè)
- 眼鏡學(xué)智慧樹知到答案2024年溫州醫(yī)科大學(xué)
評(píng)論
0/150
提交評(píng)論