📄 friendoverloadoperator.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>使用 friend 函式重载运算子</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++
Gossip: 使用 friend 函式重载运算子</a></h1>
使用类别成员来超载二元运算子时,会有一个限制,就是运算子的左边一定要是原物件,您可以使用类别成员重载运算子具有以下的功能: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">Point2D p1(10, 10);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">Point2D p2; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">p2 = p1 + 10;</span><br>
</div>
<br>
但是使用类别成员重载,您就无法使用这个方法让运算子重载有以下的功能: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">Point2D p1(10, 10);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">Point2D p2; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">p2 = 1- + p1;</span><br>
</div>
<br>
您可以规避这个问题,但每次都要让其它型态的运算元置于运算子右边也是蛮麻烦的,而且有时会容易出错,这时您可以使用friend函式来重载运算子,使用
friend函式重载二元运算子时,您要指定两个参数型态,分别表式运算子左右边的运算元型态,您可以藉由安排这两个参数来解决以上的问题,例如先如下定
义Point2D.h:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">class Point2D { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
friend Point2D operator+(const Point2D&, int); // 例如p+10 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
friend Point2D operator+(int, const Point2D&); // 例如10+p </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">private:</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
int _x;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
int _y; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}; </span><br>
</div>
<br>
再实作Ball.cpp:<br>
<div style="margin-left: 40px; font-weight: bold; font-family: Courier New,Courier,monospace;">#include
"Point2D.h"<br>
....<br>
Point2D operator+(const Point2D &p, int i) { <br>
Point2D tmp(p._x + i, p._y + i); <br>
<br>
return tmp; <br>
} <br>
<br>
Point2D operator+(int i, const Point2D &p) { <br>
Point2D tmp(i + p._x, i + p._y); <br>
<br>
return tmp; <br>
}<br>
</div>
<br>
接着您就可以直接如下进行运算:<br>
<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">Point2D p1(5, 5);</span><br style="font-weight: bold;">
<span style="font-weight: bold;">Point2D p2; </span><br style="font-weight: bold;">
<span style="font-weight: bold;">p2 = 10 + p1;</span><br>
</div>
<br>
您也可以使用friend函式来重载++或--这类的一元运算子,但要注意的是,friend不会有this指标,所以为了让它具有++或--的递增递减
原意,您要使用传参考的方式,将物件的位址告诉函式,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">class Point2D { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
friend Point2D operator++(const Point2D&); // 前置 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
friend Point2D operator++(const Point2D&, int); // 后置 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">private:</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
int _x;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
int _y; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}; </span><br>
</div>
<br>
实作时如下:<br>
<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">#include "Point2D.h"</span><br style="font-weight: bold;">
<span style="font-weight: bold;">....</span><br>
</div>
<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">Point2D
operator++(const Point2D &p) { </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
p._x++; </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
p._y++; </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
return p; </span><br style="font-weight: bold;">
<span style="font-weight: bold;">} </span><br style="font-weight: bold;">
<br style="font-weight: bold;">
<span style="font-weight: bold;">Point2D
operator++(const Point2D &p, int) { </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
Point2D tmp(p._x, p._y); </span><br style="font-weight: bold;">
<br style="font-weight: bold;">
<span style="font-weight: bold;">
p._x++; </span><br style="font-weight: bold;">
<span style="font-weight: bold;">
p._y++; </span><br style="font-weight: bold;">
<br style="font-weight: bold;">
<span style="font-weight: bold;">
return tmp; </span><br style="font-weight: bold;">
<span style="font-weight: bold;">} </span><br>
</div>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -