如何写一个运算符 [英] how to write an operator

查看:90
本文介绍了如何写一个运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更好?为什么?


class Complex {

....

friend Complex operator +(const复杂& lhs,const Complex& rhs);





朋友const复杂运算符+(const Complex& lhs,const Complex& rhs );


或者是一个而不是一个会员而不是朋友。


Thanx,

Martin < br>

解决方案

Martin Vorbrodt写道:

哪个更好?为什么?

class Complex {
...
朋友复杂算子+(const Complex& lhs,const Complex& rhs);



朋友const复合体operator +(const Complex& lhs,const Complex& rhs);
^

这个const没有增加任何价值,有些人声称它会干扰

特定类型的操作。


如果你的意思是复杂的const&',也不要这样做,因为函数

一般不应该通过引用返回值类型。

或者是其中一个而不是会员而不是朋友。




会员运营商可能无法正常平衡:


5 +复杂(8,2);


-

Phlip
http://industrialxp.org/community/bi...UserInterfaces


>那个const没有增加任何价值


好​​吧,如果你相信一些人对运营商重载的看法和做为

的整体做"然后它确实使它的行为更像是一个按值返回

内置类型的函数。


int a,b,c;

(a + b)= c; //这不应该编译


复数a,b,c;

(a + b)= c; //如果返回值不是const,这将编译。


由于我从未掌握由值返回的用户定义类型的原因不是

一个左值但它可以成为修改操作的目标。为了使

重载运算符以与内置运算符类似的方式运行,你必须使返回值为常量。


这有价值吗?


我不知道。


" DaKoadMunky" <哒********* @ aol.com>在消息中写道

Phlip

friend const复杂运算符+(const Complex& lhs ,const Complex&


rhs);

const不添加任何值



好​​吧,如果你相信什么有人说运营商超载,而b $ b就像投注一样。然后它确实使它的行为更像是一个按值返回
内置类型的函数。

int a,b,c;
(a + b)= c; //这不应该编译

复杂的a,b,c;
(a + b)= c; //如果返回值不是const,这将编译。

由于我从未掌握由值返回的用户定义类型的原因是
不是左值,但它可以是修改的目标操作。为了使
重载运算符以与内置运算符
相似的方式运行,必须使返回值为const。

这是否有价值?
<我不知道。




是的,它可以防止意外修改新返回的对象,所以可能

抓住代码编译但可能做错了。


a ++ = b;


which is preferable and WHY?

class Complex {
....
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs);

OR either one but a member operator instead of a friend.

Thanx,
Martin

解决方案

Martin Vorbrodt wrote:

which is preferable and WHY?

class Complex {
...
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs); ^
That const adds no value, and there are those who claim it interferes with
certain type-specific operations.

If you meant ''Complex const &'', don''t do that either, because functions
generally should not return value-types by reference.
OR either one but a member operator instead of a friend.



A member operator might not balance properly:

5 + Complex(8, 2);

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


>That const adds no value

Well, if you believe what some people say about operator overloading and "do as
the ints do" then it does make it behave more like a function that returns a
built-in type by value.

int a,b,c;
(a+b) = c; //This should not compile

Complex a,b,c;
(a+b) = c; //This will compile if return value is not const.

For reasons I have never grasped a user-defined type returned by value is not
an lvalue yet it can be the target of modifying operations. To make the
overloaded operator behave in a way analagous to the built-in operators you
must make the return value const.

Does this have value?

I don''t know.


"DaKoadMunky" <da*********@aol.com> wrote in message

Phlip

friend const Complex operator + (const Complex& lhs, const Complex&

rhs);
That const adds no value


Well, if you believe what some people say about operator overloading and "do as the ints do" then it does make it behave more like a function that returns a built-in type by value.

int a,b,c;
(a+b) = c; //This should not compile

Complex a,b,c;
(a+b) = c; //This will compile if return value is not const.

For reasons I have never grasped a user-defined type returned by value is not an lvalue yet it can be the target of modifying operations. To make the
overloaded operator behave in a way analagous to the built-in operators you must make the return value const.

Does this have value?

I don''t know.



Yes, it prevents accidentally modifying a newly returned object, so might
catch code which compiles but may do the wrong thing.

a++ = b;


这篇关于如何写一个运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆