赋值运算符问题 [英] assignment operator question

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

问题描述






当编写Apple类并且赋值运算符不是明确声明的
时,运算符


Apple& operator =(const Apple&)


由编译器创建。这和它之间有什么区别


const Apple& operator =(const Apple&)


除了第二种形式意味着你不能这样做:


Apple a,b ,c;

//假设有一个功能''Apple运营商+(const Apple&,const

Apple&)''


(a + b)= c; //我认为不允许使用第二个赋值运算符。

谢谢,

matthew

-


请从我的名字中删除金枪鱼一词,并在我的正确电子邮件地址中添加一个点j点(''。j。'')

。如果您是垃圾邮件发送者,请添加单词

" Istink"在金枪鱼这个词之后。


Hi,

When a class Apple is written and the assignment operator is not
explicitly declared, the operator

Apple& operator=(const Apple&)

is created by the compiler. Is there any difference between this and

const Apple& operator=(const Apple&)

other than the second form means that you can''t do this:

Apple a, b, c;
//Assume there is a function ''Apple operator+(const Apple&, const
Apple&)''

(a + b) = c; //not allowed with second assignment operator, I think.
thanks,
matthew
--

Please remove the word ''tuna'' from my name and add a dot j dot (''.j.'')
for my correct email address. If you are a spammer please add the word
"Istink" after the word tuna.

推荐答案



" Matthew Polder" <毫安*************** @ kodak.com>在留言中写道

新闻:40 *************** @ kodak.com ...

"Matthew Polder" <ma***************@kodak.com> wrote in message
news:40***************@kodak.com...



当编写Apple类并且未明确声明赋值运算符时,运算符

Apple& operator =(const Apple&)

由编译器创建。这和它之间是否存在差异?

const Apple& operator =(const Apple&)

除了第二种形式意味着你不能这样做:

Apple a,b,c;
/ /假设有一个功能''Apple运营商+(const Apple&,const
Apple&)''

(a + b)= c; //我认为不允许使用第二个赋值运算符。

Hi,

When a class Apple is written and the assignment operator is not
explicitly declared, the operator

Apple& operator=(const Apple&)

is created by the compiler. Is there any difference between this and

const Apple& operator=(const Apple&)

other than the second form means that you can''t do this:

Apple a, b, c;
//Assume there is a function ''Apple operator+(const Apple&, const
Apple&)''

(a + b) = c; //not allowed with second assignment operator, I think.




任何形式的赋值运算符都不允许这样做,假设

a运营商的有效声明+。


不同的是这个


(a = b)= c


使用第二种形式(a = b)返回一个const引用,然后在另一个运算符的左侧返回

=(因为operator =不是const

方法)。


(a = b)= c对于像int这样的内置类型是没用的,但它是合法的,

所以为什么不呢同样的事情对于用户定义的类也是合法的吗?


john



No this would be allowed by either form of the assignment operator, assuming
a valid declaration of operator+.

The difference is this

(a = b) = c

With the second form (a = b) returns a const reference which cannot then go
on the left hand side of another operator= (because operator= is not a const
method).

(a = b) = c is pretty useless for built in types like int, but it is legal,
so why not make the same thing legal for user defined classes as well?

john




"马修波尔德 <毫安*************** @ kodak.com>在留言中写道

新闻:40 *************** @ kodak.com ...

"Matthew Polder" <ma***************@kodak.com> wrote in message
news:40***************@kodak.com...



当编写Apple类并且未明确声明赋值运算符时,运算符

Apple& operator =(const Apple&)

由编译器创建。这和它之间是否存在差异?

const Apple& operator =(const Apple&)

除了第二种形式意味着你不能这样做:

Apple a,b,c;
/ /假设有一个功能''Apple运营商+(const Apple&,const
Apple&)''

Hi,

When a class Apple is written and the assignment operator is not
explicitly declared, the operator

Apple& operator=(const Apple&)

is created by the compiler. Is there any difference between this and

const Apple& operator=(const Apple&)

other than the second form means that you can''t do this:

Apple a, b, c;
//Assume there is a function ''Apple operator+(const Apple&, const
Apple&)''




从右到左分配运营商组。 />
a = b = c = d,我希望a和d具有相同的值。是否有可能返回

类型是const Apple& ?



Assignment operator groups right to left.
a=b=c=d, i would expect a and d to have same values. Was it possible if return
type was const Apple& ?




" Sharad Kala" <无***************** @ yahoo.com>在消息中写道

新闻:2j ************* @ uni-berlin.de ...

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2j*************@uni-berlin.de...


a = b = c = d,我希望a和d具有相同的值。如果返回
类型是const Apple& ?


a=b=c=d, i would expect a and d to have same values. Was it possible if return
type was const Apple& ?




对不起,实际应该是((a = b)= c)= d。



Sorry, actually it should be ((a=b)=c)=d.

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

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