C++ 什么是赋值运算符的返回类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15292892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:14:20  来源:igfitidea点击:

what is return type of assignment operator?

c++referenceassignment-operatordereferencelvalue

提问by oczkoisse

I am just starting C++. I am a bit confused about 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".

我刚开始使用 C++。我对赋值和取消引用运算符的返回类型有点困惑。我正在关注 C++ Primer 这本书。在各种场合,作者说赋值运算符的返回类型是对左手操作数类型的引用,但后来又说返回类型是左手操作数的类型。我已经提到了 C++11 Standard Sec。5.17,其中返回类型被描述为“左值引用左手操作数”。

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.

这些陈述是等价的吗?如果是,那又如何?任何解释将不胜感激。

回答by SomeWittyUsername

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.

该标准正确定义了赋值运算符的返回类型。实际上,赋值操作本身并不依赖于返回值——这就是为什么返回类型不容易理解的原因。

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. cshould be assigned into band binto a. Rewrite this as a.operator=(b.operator=(c)). In order for the assignment into ato 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).

返回类型对于链接操作很重要。考虑以下构造:a = b = c;. 这应该等于a = (b = c),即c应该被分配到bb进入a。将其重写为a.operator=(b.operator=(c)). 为了使赋值a正确工作,返回类型b.operator=(c)必须是对内部赋值结果的引用(它也适用于复制,但这只是不必要的开销)。

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

取消引用运算符返回类型取决于您的内部逻辑,以适合您需要的方式定义它。

回答by Luchian Grigore

They can both be anything, but usuallyoperator =returns the current object by reference, i.e.

它们都可以是任何东西,但通常operator =通过引用返回当前对象,即

A& A::operator = ( ... )
{
   return *this;
}

And yes, "reference to the type of left hand operand" and "lvalue referring to left hand operand" mean the same thing.

是的,“引用左手操作数的类型”和“引用左手操作数的左值”是同一个意思。

The dereference operator can have basically any return type. It mostly depends on the logic of the program, because you're overloading the operator that applies to an object, not to a pointer to the object. Usually, this is used for smart pointers, or iterators, and return the object they wrap around:

解引用运算符基本上可以有任何返回类型。它主要取决于程序的逻辑,因为您正在重载适用于对象的运算符,而不是指向对象的指针。通常,这用于智能指针或迭代器,并返回它们环绕的对象:

struct smart_ptr
{
   T* innerPtr;
   T* smart_ptr::operator* ()
   {
      return innerPtr;
   }
}

smart_ptr p; 
T* x = *p;  

回答by bash.d

I have seen similar issues, but I guess it would be best to use

我见过类似的问题,但我想最好使用

X& X::operator=(const X&);

Using this, you will be able to reuse the object in a chain-assignment.

使用它,您将能够在链分配中重用该对象。