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