重載的運(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}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注