運算符重載,就是對已有的運算符重新進行定義,賦予其另一種功能,以適應不同的數據類型。
你可以重定義或重載大部分 C++ 內置的運算符。例如 + 、 - 、 * 、 / 、
++、--、>>、<<等,這樣,你就能使用自定義類型的運算符。
運算符重載的基本格式
重載的運算符是帶有特殊名稱的函數,函數名是由關鍵字 operator 和
其后要重載的運算符符號構成的。與其他函數一樣,重載運算符有一個
返回類型和一個參數列表。
Point operator+(const Point &);
運算符重載有兩種方式:一種是類內重載(運算符重載函數作為類的成員函數),另一種是類外重載(運算符重載函數作為類的友元函數)
類內重載
#include <iostream>
using namespace std;
class Point{
public:
Point(){};
Point (int x, int y): x(x),y(y) {};
Point operator+(const Point &a){ //類內重載,運算符重載函數作為類的成員函數
Point ret;
ret.x = this->x + a.x;
ret.y = this->y + a.y;
return ret;
}
int x,y;
};
int main() {
Point a(2,4),b(5,3);
Point c = a + b;
cout<< "x :" << c.x << endl;
cout<<"y :" << c.y << endl;
}
當上面的代碼被編譯和執行時,它會產生下列結果:
x : 7
y: 7
運算符重載是類內重載時,運算符重載函數作為類的成員函數,以上述代碼為例 a + b 相當於 a 對象調用+方法並且傳入參數時 b 對象
類外重載
#include <iostream>
using namespace std;
class Point{
public:
Point(){};
Point (int x, int y): x(x),y(y) {};
friend Point operator+(const Point &, const Point &);
int x,y;
};
Point operator+(const Point &a,const Point &b){//類外重載,運算符重載函數作為類的友元函數
Point ret;
ret.x = a.x + b.x;
ret.y = a.y + b.y;
return ret;
}
int main() {
Point a(2,4),b(5,3);
Point c = a + b;
cout<< "x :" << c.x << endl;
cout<<"y :" << c.y << endl;
}
當上面的代碼被編譯和執行時,它會產生和上面一樣的結果
各種運算符重載實例
下面將進行各種運算符重載實例的代碼演示,演示幾種基本的運算符重載。
插入運算符重載>> and 提取運算符重載<<
以提取運算符重載<<
為例,cout
是 ostream
類的對象。ostream
類和 cout
都是在頭文件 <iostream>
中聲明的。ostream
類將<<
重載為成員函數。
下面我們重載<<
使用cout
輸出a對象
#include <iostream>
using namespace std;
class Point{
public:
Point(){};
Point (int x, int y): x(x),y(y) {};
friend Point operator+(const Point &, const Point &);
friend ostream &operator<<(ostream &out , const Point &a);
private:
int x,y;
};
Point operator+(const Point &a,const Point &b){
Point ret;
ret.x = a.x + b.x;
ret.y = a.y + b.y;
return ret;
}
ostream &operator<<(ostream &out , const Point &a){
out << "<Point>( " << a.x << ", " << a.y << ")";
return out;
}
int main() {
Point a(2,4),b(5,3);
Point c = a + b;
cout << c<< endl;
}
當上面的代碼被編譯和執行時,它會產生下列結果:
< Point>( 7, 7)
注意:重載<<
時,是類外重載,習慣上人們是使用 cin>>
和 cout<<
的,得使用友元函數來重載運算符,如果使用成員函數來重載會出現 c<<cout;
這種不自然的代碼。
另外應該會有人對ostream &operator<<(ostream &out , const Point &a)
函數感到疑惑,首先在重載<<
時,返回值類型是ostream&
, 第一個參數也是ostream&
。也就是說,表達式cout<<c
的返回值仍是 cout
,所以cout<<c<<endl;
才能成立
前置運算符重載++ and 后置運算符重載++
#include <iostream>
using namespace std;
class Point{
public:
Point(){};
Point (int x, int y): x(x),y(y) {};
friend Point operator+(const Point &, const Point &);
friend ostream &operator<<(ostream &out , const Point &a);
Point& operator++(){ //前置運算符,需要引用返回,不需要參數。返回自增后的值,且返回的是一個左值
x++;
y++;
return *this;
}
const Point operator++(int){//后置++,不需要引用返回,需要參數區分。返回自增前的值,且返回的是一個右值
Point temp(x,y);
x++;
y++;
return temp;
}
private:
int x,y;
};
Point operator+(const Point &a,const Point &b){
Point ret;
ret.x = a.x + b.x;
ret.y = a.y + b.y;
return ret;
}
ostream &operator<<(ostream &out , const Point &a){
out << "<Point>(" << a.x << " , " << a.y << ")";
return out;
}
int main() {
Point a(2,4),b(5,3);
Point c = a + b;
cout << c << endl;
c++;
cout << c << endl;
++c;
cout << c << endl;
}
當上面的代碼被編譯和執行時,它會產生下列結果:
(7 , 7)
< Point>(8 , 8)
< Point>(9 , 9)
1>為區別前置和后置運算符,需要在后置運算符重載函數中加參數“int”,雖然這個類型在此除了以示區別之外並不代表任何實際含義;
2>前置返回的是變量的引用,后置返回的是常量。所以++++c合法,而c++++不合法;
3>為什么不讓c++++也合法呢?如果要實現c++++合法,必須使后置返回變量或變量的引用。c++是先返回c值再+1,所以不可能返回c,那就只能先建立局部變量來保存c的初值,然后再返回局部變量(局部變量不允許返回引用),但返回了局部變量之后,如果再連着進行下一次++運算,參與運算的就是這個局部變量的值了,所以此時c++++其實等效與c++,也就沒有存在的意義了。