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

首頁 > 學院 > 開發設計 > 正文

操作重載

2019-11-14 09:47:20
字體:
來源:轉載
供稿:網友

重載的運算符是具有特殊名字的函數。它們的名字由關鍵字Operator和其后要定義的運算符號共同組成。當一個重載的運算符是成員函數時,this綁定到左側運算對象。成員運算符函數的參數數量比運算對象少一個。 不能被重載的運算符有(::) (.* )(. )(?:) 不應該被重載的運算符: 邏輯運算符,逗號運算符,取地址運算符


選做成員函數的運算符: =,[ ],( ), ->, 符合賦值+=,-=.. , ++,–,*(解引用) 改變對象的狀態或與給定類型密切相關的運算符


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


重載輸入和輸出運算符: 必須為非成員函數,輸出運算符應該主要負責打印對象的內容而非控制格式,輸出運算符不應該打印換行符。輸入運算符必須處理輸入可能失敗的情況,而輸出運算符不需要。

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

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

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

重載下標運算符: 成員函數,如果一個類包含下標運算符,則它通常會定義兩個版本,一個返回普通引用,另一個類返回常量引用。

#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;//錯誤 return 0;}

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

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

重載成員訪問運算符,即*和-> 成員函數(不總是),*返回對象的引用,->返回對象的地址。

#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}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美日韩手机在线观看 | 毛片视频网站 | 欧美一级aa免费毛片 | 毛片中文字幕 | 中文字幕激情 | 色人阁在线视频 | 日本网站一区 | 国产精品视频一区二区三区四区五区 | 日日摸夜夜骑 | 永久免费黄色片 | 高清成人在线 | 成人午夜淫片a | 成人国产精品久久 | 亚洲一区在线视频观看 | 在线成人免费av | 免费在线观看国产 | 国产自在线 | 一区二区三区四区视频在线观看 | 国产在线播放一区二区 | 久久久资源网 | 欧美精品免费一区二区三区 | 免费午夜视频 | 亚州综合图片 | 国产一区亚洲 | 91精品国产手机 | 久久久久久久91 | 国产乱淫a∨片免费观看 | 日韩视频高清 | 国产乱一区二区三区视频 | 久草在线综合 | 精品国产高清一区二区三区 | 成人毛片免费视频 | 国产午夜亚洲精品理论片大丰影院 | 日本黄色免费播放 | 亚洲精品一区二区三区免 | 男女污污视频网站 | 国内精品久久久久久2021浪潮 | 久久亚洲精品视频 | 99最新地址 | 亚洲成人入口 | 欧美精品国产综合久久 |