什么是返回类型的赋值运算符? [英] what is return type of assignment operator?

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

问题描述

我刚刚开始C ++。除了我对返回类型的赋值和取消引用运算符感到困惑之外,所有都很好。我跟着这本书C ++ Primer。在各种情况下,作者说返回类型的赋值操作符是指左手操作数的类型,但后来,他说返回类型是左手操作数的类型。我已经参考C ++ 11标准秒。 5.17,其中返回类型被描述为左值引用左手操作数。

I am just starting C++. All is fine except that I am confused on the return type of assignment and dereference operator. I am following the book C++ Primer. At various occasions, the author says that the return type of assignment operator is reference to the type of left hand operand but later on, he says that the return type is the type of the left hand operand.I have referred C++11 Standard Sec. 5.17, where the return type is described as "lvalue referring to left hand operand".

同样,我不知道dereference是否返回指向的对象,对对象的引用。

Similarly, I can't figure out whether dereference returns the pointed-to object or the reference to the object.

这些语句是否等效?如果是这样,那么如何?

Are these statements equivalent?If so, then how? Any explanation would be appreciated.

推荐答案

标准正确定义了赋值运算符的返回类型。
实际上,赋值操作本身不依赖于返回值 - 这就是为什么返回类型不直接理解。

The standard correctly defines the return type of an assignment operator. Actually, the assignment operation itself doesn't depend on the return value - that's why the return type isn't straightforward to understanding.

返回类型是对于链接操作很重要。
考虑下面的结构: a = b = c; 。这应该等于 a =(b = c),即 c 应分配到 b b 转换为 a 。重写为 a.operator =(b.operator =(c))。为了使赋值 a 正确工作, b.operator =(c)的返回类型必须是引用

The return type is important for chaining operations. Consider the following construction: a = b = c;. This should be equal to a = (b = c), i.e. c should be assigned into b and b into a. Rewrite this as a.operator=(b.operator=(c)). In order for the assignment into a to work correctly the return type of b.operator=(c) must be reference to the inner assignment result (it will work with copy too but that's just an unnecessary overhead).

取消引用操作符返回类型取决于你的内部逻辑,在它的内部定义适合您的需要。

The dereference operator return type depends on your inner logic, define it in the way that suits your needs.

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

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