Target:將物件兩者用自身定義做處理
大部份的運算子都是可以被重載的,除了以下的運算子之外:
. :: .* ?:
在重載+與-號運算子時,所接收的物件引數來自被重載的運算子右邊,例如在程式碼中加法運算時,+右邊是p2,所以傳入的物件引數就是p2物件,減法運算 時-號右邊是p1,所以傳入的就是p1物件
公式: 回傳型態 operator#(要輸入的物件);
class Point2D {
public:
Point2D();
Point2D(int, int);
int x() {return _x;}
int y() {return _y;}
Point2D operator+(const Point2D&); // 重載+運算子
Point2D operator-(const Point2D&); // 重載-運算子
Point2D& operator++(); // 重載++前置,例如 ++p
Point2D operator++(int); // 重載++後置,例如 p++
Point2D& operator--(); // 重載--前置,例如 --p
Point2D operator--(int); // 重載--後置,例如 p--
private:
int _x;
int _y;
};
- Point2D.cpp
#include "Point2D.h"
Point2D::Point2D() {
_x = 0;
_y = 0;
}
Point2D::Point2D(int x, int y) {
_x = x;
_y = y;
}
Point2D Point2D::operator+(const Point2D &p) {
Point2D tmp(_x + p._x, _y + p._y);
return tmp;
}
Point2D Point2D::operator-(const Point2D &p) {
Point2D tmp(_x - p._x, _y - p._y);
return tmp;
}
Point2D& Point2D::operator++() {
_x++;
_y++;
return *this;
}
Point2D Point2D::operator++(int) {
Point2D tmp(_x, _y);
_x++;
_y++;
return tmp;
}
Point2D& Point2D::operator--() {
_x--;
_y--;
return *this;
}
Point2D Point2D::operator--(int) {
Point2D tmp(_x, _y);
_x--;
_y--;
return tmp;
}
- main.cpp
#include
#include "Point2D.h"
using namespace std;
int main() {
Point2D p1(5, 5);
Point2D p2(10, 10);
Point2D p3;
p3 = p1 + p2;
cout << "p3(x, y) = ("
<< p3.x() << ", " << p3.y()
<< ")" << endl;
p3 = p2 - p1;
cout << "p3(x, y) = ("
<< p3.x() << ", " << p3.y()
<< ")" << endl;
p3 = ++p1;
cout << "p3(x, y) = ("
<< p3.x() << ", " << p3.y()
<< ")" << endl;
return 0;
}
執行結果:
p3(x, y) = (15, 15)
p3(x, y) = (5, 5)
p3(x, y) = (6, 6)
另外,重載運算符存在一個問題,原函數必須置左,因此以下並非合法
Point2D p1(10, 10);
Point2D p2;
p2 = 1 + p1;
Point2D p2;
p2 = 1 + p1;
為了解決問題,friend 友元予以協助:
class Point2D {
public:
....
friend Point2D operator+(const Point2D&, int); // 例如p+10
friend Point2D operator+(int, const Point2D&); // 例如10+p
private:
int _x;
int _y;
};
public:
....
friend Point2D operator+(const Point2D&, int); // 例如p+10
friend Point2D operator+(int, const Point2D&); // 例如10+p
private:
int _x;
int _y;
};
#include "Point2D.h"
....
Point2D operator+(const Point2D &p, int i) {
Point2D tmp(p._x + i, p._y + i);
return tmp;
}
Point2D operator+(int i, const Point2D &p) {
Point2D tmp(i + p._x, i + p._y);
return tmp;
}
....
Point2D operator+(const Point2D &p, int i) {
Point2D tmp(p._x + i, p._y + i);
return tmp;
}
Point2D operator+(int i, const Point2D &p) {
Point2D tmp(i + p._x, i + p._y);
return tmp;
}
- 也可以使用friend函式來重載++或--這類的一元運算子,但要注意的是,friend不會有this指標,所以為了讓它具有++或--的遞增遞減 原意,您要使用傳參考的方式,將物件的位址告訴函式,例如:
class Point2D {
public:
....
friend Point2D operator++(Point2D&); // 前置
friend Point2D operator++(Point2D&, int); // 後置
private:
int _x;
int _y;
};
public:
....
friend Point2D operator++(Point2D&); // 前置
friend Point2D operator++(Point2D&, int); // 後置
private:
int _x;
int _y;
};
實作時如下:
#include "Point2D.h"
....
....
Point2D operator++(Point2D &p) {
p._x++;
p._y++;
return p;
}
Point2D operator++(Point2D &p, int) {
Point2D tmp(p._x, p._y);
p._x++;
p._y++;
return tmp;
}
p._x++;
p._y++;
return p;
}
Point2D operator++(Point2D &p, int) {
Point2D tmp(p._x, p._y);
p._x++;
p._y++;
return tmp;
}
沒有留言:
張貼留言