C++之运算符重载(2)
上一節主要講解了C++里運算符重載函數,在看了單目運算符(++)重載的示例后,也許有些朋友會問這樣的問題。++自增運算符在C或C++中既可以放在操作數之前,也可以放在操作數之后,但是前置和后置的作用又是完全不同的(q前置運算符:先加1,再賦值;后置運算符:先賦值,再加1)。那么要怎么重載它們,才可以有效的區分開來呢?今天我就來說說C++中是怎么處理前置運算符和后置運算符的重載的。以及介紹一下插入運算符(>>)和提取運算符(<<)的重載。
1.在C++里編譯器是根據運算符重載函數參數表里是否插入關鍵字int來區分前置還是后置運算。比如:
#include "stdafx.h" #include <iostream>class TDPoint//三維坐標 { private: int x; int y; int z; public:TDPoint(int x=0,int y=0,int z=0){ this->x=x; this->y=y; this->z=z;}TDPoint operator++();//成員函數重載前置運算符++TDPoint operator++(int);//成員函數重載后置運算符++friend TDPoint operator++(TDPoint& point);//友元函數重載前置運算符++friend TDPoint operator++(TDPoint& point,int);//友元函數重載后置運算符++ void showPoint(); };TDPoint TDPoint::operator++() { ++this->x; ++this->y; ++this->z; return*this;//返回自增后的對象 }TDPoint TDPoint::operator++(int) {TDPoint point(*this); this->x++; this->y++; this->z++; return point;//返回自增前的對象 }TDPoint operator++(TDPoint& point) { ++point.x; ++point.y; ++point.z; return point;//返回自增后的對象 }TDPoint operator++(TDPoint& point,int) {TDPoint point1(point);point.x++;point.y++;point.z++; return point1;//返回自增前的對象 }void TDPoint::showPoint() {std::cout<<"("<<x<<","<<y<<","<<z<<")"<<std::endl; }int main() {TDPoint point(1,1,1);point.operator++();//或++pointpoint.showPoint();//前置++運算結果 point=point.operator++(0);//或point=point++point.showPoint();//后置++運算結果operator++(point);//或++point;point.showPoint();//前置++運算結果 point=operator++(point,0);//或point=point++;point.showPoint();//后置++運算結果 return0; }結果:
從示例代碼里可以清楚的看出,后置運算符重載函數比前置運算符重載函數多了一個int類型的參數,這個參數只是為了區別前置和后置運算符,此外沒有任何作用。所以在調用后置運算符重載函數時,int類型的實參可以取任意值。
2.在C++中,操作符"<<"和">>"被定義為左位移運算符和右位移運算符。由于在iostream頭文件中對它們進行了重載,使得它們可以用基本數據的輸出和輸入。
#include "stdafx.h" #include <iostream>int main() {int a=10;std::cout<<"a="<<a<<std::endl;//運算符"<<"重載后用于輸出a=a>>2;//右移運算符std::cout<<"右移2位:a="<<a<<std::endl;std::cout<<"請輸入一個整數a:";std::cin>>a;//運算符">>"重載后用于輸入a=a<<2;//左移運算符std::cout<<"左移2位:a="<<a<<std::endl;return0; }結果:
插入運算符"<<"是雙目運算符,左操作數為輸出流類ostream的對象,右操作數為系統預定義的基本類型數據。頭文件iostrem對其重載的函數原型為ostream& operator<<(ostream& ,類型名);類型名就是指基本類型數據。但如果要輸出用戶自定義的類型數據的話,就需要重載操作符"<<",因為該操作符的左操作數一定為ostream類的對象,所以插入運算符"<<"只能是類的友元函數或普通函數,不能是其他類的成員函數。一般定義格式:
ostream& operator<<(ostream& ,自定義類名&);
提取運算符">>"也是如此,左操作數為istream類的對象,右操作數為基本類型數據。頭文件iostrem對其重載的函數原型為istream& operator>>(istream& ,類型名);提取運算符也不能作為其他類的成員函數,可以是友元函數或普通函數。它的一般定義格式為:
istream& operator>>(istream& ,自定義類名&);
我還是用上一節用的Complex類(復數類)來舉例:
#include "stdafx.h" #include <iostream>class Complex //復數類 {private://私有 double real;//實數 double imag;//虛數 public:Complex(double real=0,double imag=0){this->real=real;this->imag=imag;}friend std::ostream&operator<<(std::ostream& o,Complex& com);//友元函數重載提取運算符"<<"friend std::istream&operator>>(std::istream& i,Complex& com);//友元函數重載插入運算符">>" };std::ostream&operator<<(std::ostream& o,Complex& com) {std::cout<<"輸入的復數:";o<<com.real;if(com.imag>0)o<<"+";if(com.imag!=0)o<<com.imag<<"i"<<std::endl;return o; }std::istream&operator>>(std::istream& i,Complex& com) {std::cout<<"請輸入一個復數:"<<std::endl;std::cout<<"real(實數):";i>>com.real;std::cout<<"imag(虛數):";i>>com.imag;return i; }int main() {Complex com;std::cin>>com;std::cout<<com;return0; }結果:
總結
以上是生活随笔為你收集整理的C++之运算符重载(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LINQ TO ENTITY 根据Bir
- 下一篇: 老男孩为网友工作疑难问题解答一例