麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

操作重載

2019-11-14 09:19:37
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

重載的運(yùn)算符是具有特殊名字的函數(shù)。它們的名字由關(guān)鍵字Operator和其后要定義的運(yùn)算符號(hào)共同組成。當(dāng)一個(gè)重載的運(yùn)算符是成員函數(shù)時(shí),this綁定到左側(cè)運(yùn)算對(duì)象。成員運(yùn)算符函數(shù)的參數(shù)數(shù)量比運(yùn)算對(duì)象少一個(gè)。 不能被重載的運(yùn)算符有(::) (.* )(. )(?:) 不應(yīng)該被重載的運(yùn)算符: 邏輯運(yùn)算符,逗號(hào)運(yùn)算符,取地址運(yùn)算符


選做成員函數(shù)的運(yùn)算符: =,[ ],( ), ->, 符合賦值+=,-=.. , ++,–,*(解引用) 改變對(duì)象的狀態(tài)或與給定類(lèi)型密切相關(guān)的運(yùn)算符


選做非成員函數(shù)的運(yùn)算符: 算數(shù)+,-,*,/… 相等性== ,!= 關(guān)系 >,<,>=,<=.. 位運(yùn)算符 ^, |, &


重載輸入和輸出運(yùn)算符: 必須為非成員函數(shù),輸出運(yùn)算符應(yīng)該主要負(fù)責(zé)打印對(duì)象的內(nèi)容而非控制格式,輸出運(yùn)算符不應(yīng)該打印換行符。輸入運(yùn)算符必須處理輸入可能失敗的情況,而輸出運(yùn)算符不需要。

#include <string>class A{ friend std::ostream& operator<<(std::ostream& os,const A &a); friend std::istream& operator>>(std::istream& is,A &a);public: A(int a=10,std::string s="initial"):value(a),s(s){}PRivate: int value; std::string s;};std::ostream& operator<<(std::ostream& os,const A &a){ os<<a.value<<a.s; return os;}std::istream& operator>>(std::istream& is,A &a){ is>>a.value>>a.s; if(!is) { a=A(); std::cerr<<"invalid input!/n"; } return is; }int main(){ A a; std::cin>>a; std::cout<<a<<std::endl; return 0;}

重載相等與不相等運(yùn)算符: 非成員函數(shù),需具有傳遞性,定義了其中一個(gè)一般需要定義另一個(gè),而通常運(yùn)算符的一個(gè)應(yīng)該把工作委托給另一個(gè)。

#include <iostream>#include <string>class A{ friend bool operator==(const A& a1,const A &a2); friend bool operator!=(const A& a1,const A &a2);public: A(int a=10,std::string s="initial"):value(a),s(s){}private: int value; std::string s;};bool operator==(const A& a1,const A &a2){ return a1.value==a2.value&&a1.s==a2.s;}bool operator!=(const A& a1,const A &a2){ return !(a1==a2);}int main(){ A a1,a2(11,"apple"),a3; std::cout<<(a1==a2)<<std::endl; std::cout<<(a1==a3)<<std::endl; std::cout<<(a1!=a2)<<std::endl; std::cout<<(a1!=a3)<<std::endl; return 0;}

重載下標(biāo)運(yùn)算符: 成員函數(shù),如果一個(gè)類(lèi)包含下標(biāo)運(yùn)算符,則它通常會(huì)定義兩個(gè)版本,一個(gè)返回普通引用,另一個(gè)類(lèi)返回常量引用。

#include <iostream>#include <string>class A{public: A():value(new int[8]){} int& operator[](std::size_t n){return value[n];} const int &operator[](std::size_t n)const {return value[n];}private: int *value;};int main(){ A a; a[5]=8; std::cout<<a[5]<<std::endl; const A a1; //a1[2]=1;//錯(cuò)誤 return 0;}

重載遞增和遞減運(yùn)算符: 成員函數(shù),有前置和后置版本,前置返回引用,后置返回值。

#include <iostream>class A{ friend std::ostream& operator<<(std::ostream& os,const A &a);public: A(int a=0):value(a){}; A & operator++(){ value++; return *this; }//前置版本++ A operator++(int){ int tmp=value; value++; return *this; }//后置版本++ A &operator--(){ value--; return *this; }//前置版本-- A operator--(int){ int tmp=value; value--; return *this; }//后置版本--private: int value;};std::ostream& operator<<(std::ostream& os,const A &a){ os<<a.value; return os;}int main(){ A a(8); ++a; std::cout<<a<<std::endl;//9 --a; std::cout<<a<<std::endl;//8 a.operator++(); std::cout<<a<<std::endl;//9 a.operator--(); std::cout<<a<<std::endl;//8 return 0;}

重載成員訪(fǎng)問(wèn)運(yùn)算符,即*和-> 成員函數(shù)(不總是),*返回對(duì)象的引用,->返回對(duì)象的地址。

#include <iostream>class A{public: A(int a=0):value(a){}; A & operator++(){ value++; return *this; }//前置版本++ A operator++(int){ int tmp=value; value++; return *this; }//后置版本++ A &operator--(){ value--; return *this; }//前置版本-- A operator--(int){ int tmp=value; value--; return *this; }//后置版本-- int value;};class A_ptr{public: A_ptr(A &a):a_ptr(&a){}; A &operator*() const{return *a_ptr;} A *operator->() const {return a_ptr;} A *a_ptr;};int main(){ A a(8); A_ptr ap(a); std::cout<<ap->value<<std::endl;//8 ap->operator++(); //++a std::cout<<ap->value<<std::endl;//9 std::cout<<(*ap).value<<std::endl;//9}
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久99精品久久 | 日韩av在线网址 | 韩国草草影院 | 久久精品欧美一区二区三区不卡 | 欧美成人影院 | 欧美日本在线视频 | 国产精品久久久久久模特 | 黄色网址在线播放 | av电影手机在线看 | 黄色片网站免费在线观看 | 草操影院 | 亚洲欧美不卡视频 | 日本成年网| 亚洲网站在线观看视频 | 久久精品一区二区三区不卡牛牛 | 中国av中文字幕 | 国产成人精品免费视频大全办公室 | 久久精品99久久久久久2456 | 沉沦的校花奴性郑依婷c到失禁 | 污片视频网站 | 成年人国产视频 | 精品无码一区在线观看 | 小情侣嗯啊哦视频www | 一区二区三区视频在线 | 国产成年人在线观看 | 激情视频免费看 | 成人免费在线播放 | 精品国产九九九 | 欧美一区二区精品夜夜嗨 | 欧美亚洲国产成人综合在线 | www.成人在线 | 黄wwww | 国产成人精品免费视频大全最热 | 国产精品91在线 | 成人福利视频网站 | 国产女厕一区二区三区在线视 | 国产一区网址 | 国产伊人色 | 国产乱淫av片免费 | 欧美成人精品欧美一级乱黄 | 久久国产精品久久久久久 |