標籤:
1.在C++裡編譯器是根據運算子多載函數參數表裡是否插入關鍵字int來區分前置還是後置運算。比如:
1 #include "stdafx.h"
2 #include <iostream>
3
4 class TDPoint//三維座標
5 {
6 private:
7 int x;
8 int y;
9 int z;
10 public:
11 TDPoint(int x=0,int y=0,int z=0)
12 {
13 this->x=x;
14 this->y=y;
15 this->z=z;
16 }
17 TDPoint operator++();//成員函數重載前置運算子++
18 TDPoint operator++(int);//成員函數重載後置運算子++
19 friend TDPoint operator++(TDPoint& point);//友元函數重載前置運算子++
20 friend TDPoint operator++(TDPoint& point,int);//友元函數重載後置運算子++
21 void showPoint();
22 };
23
24 TDPoint TDPoint::operator++()
25 {
26 ++this->x;
27 ++this->y;
28 ++this->z;
29 return*this;//返回自增後的對象
30 }
31
32 TDPoint TDPoint::operator++(int)
33 {
34 TDPoint point(*this);
35 this->x++;
36 this->y++;
37 this->z++;
38 return point;//返回自增前的對象
39 }
40
41 TDPoint operator++(TDPoint& point)
42 {
43 ++point.x;
44 ++point.y;
45 ++point.z;
46 return point;//返回自增後的對象
47 }
48
49 TDPoint operator++(TDPoint& point,int)
50 {
51 TDPoint point1(point);
52 point.x++;
53 point.y++;
54 point.z++;
55 return point1;//返回自增前的對象
56 }
57
58 void TDPoint::showPoint()
59 {
60 std::cout<<"("<<x<<","<<y<<","<<z<<")"<<std::endl;
61 }
62
63 int main()
64 {
65 TDPoint point(1,1,1);
66 point.operator++();//或++point
67 point.showPoint();//前置++運算結果
68
69 point=point.operator++(0);//或point=point++
70 point.showPoint();//後置++運算結果
71
72 operator++(point);//或++point;
73 point.showPoint();//前置++運算結果
74
75 point=operator++(point,0);//或point=point++;
76 point.showPoint();//後置++運算結果
77
78 return0;
79 }
結果:
從範例程式碼裡可以清楚的看出,後置運算子多載函數比前置運算子多載函數多了一個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;
}
結果:
出處:http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/13/2137033.html
[轉]C++之運算子多載(2)