国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

操作重載

2019-11-14 09:47:20
字體:
供稿:網(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)或與給定類型密切相關(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è)類包含下標(biāo)運(yùn)算符,則它通常會(huì)定義兩個(gè)版本,一個(gè)返回普通引用,另一個(gè)類返回常量引用。

#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;}

重載成員訪問運(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)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 重庆市| 阿拉善左旗| 奉化市| 安陆市| 保靖县| 包头市| 盐城市| 瓦房店市| 松原市| 绥德县| 农安县| 义马市| 古蔺县| 武威市| 竹山县| 富民县| 钟祥市| 呼伦贝尔市| 兴国县| 福泉市| 威远县| 江口县| 荣昌县| 昆山市| 牟定县| 邯郸县| 基隆市| 崇仁县| 洛浦县| 漳浦县| 南雄市| 西充县| 江油市| 衡阳市| 洞口县| 襄垣县| 闽清县| 武威市| 阜康市| 北流市| 黄平县|