operator 与 &operator 在 c++中具体有什么区别

能举一些例子吗?... 能举一些例子吗? 展开
 我来答
happyxgy
推荐于2018-05-15 · 超过10用户采纳过TA的回答
知道答主
回答量:33
采纳率:0%
帮助的人:0
展开全部
下面是一个+号的重载操作:
#include <iostream>
using namespace std;

//声明
class Point;
Point operator+(Point &a,Point &b);
//Point &operator+(Point &a,Point &b);

//定义点类
class Point
{
public:
int x,y;
Point(){}
Point(int xx,int yy){x=xx;y=yy;}
void print(){ cout<<"("<<x<<","<<y<<")\n"; }
friend Point operator+(Point &a,Point &b);
//friend Point &operator+(Point &a,Point &b);
};

//重载+号操作(返回值)
Point operator+( Point &a,Point &b)
{
Point s(a.x+b.x,a.y+b.y);
return s;
}

//重载+号操作(返回址)
/*Point &operator+( Point &a,Point &b)
{
a.x+=b.x;
a.y+=b.y;
return a;
}*/

//主函数
void main()
{
Point a(3,2),b(1,5),c;
c=a+b;
c.print();
}
将注释替换过来,就可以得到&operator+的重载了
shfhere
2007-04-22 · TA获得超过108个赞
知道答主
回答量:99
采纳率:0%
帮助的人:65.5万
展开全部
重载操作符时候,返回值的址和值的区别的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2020-04-28
展开全部
&operator
可以实现连用
比如定义complex 类,
complex a,b,c;
可以实现cout<<a<<b<<c;
不用引用无法实现连用。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式