简体   繁体   English

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

[英]what is return type of assignment operator?

I am just starting C++. 我刚开始使用C ++。 I am a bit confused about the return type of assignment and dereference operator. 我对赋值和取消引用运算符的返回类型有些困惑。 I am following the book C++ Primer. 我正在关注《 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. 我已经提到了C ++ 11 Standard Sec。 5.17, where the return type is described as "lvalue referring to left hand operand". 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. 任何解释将不胜感激。

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; 考虑以下结构: a = b = c; . This should be equal to a = (b = c) , ie c should be assigned into b and b into a . 这应该等于a = (b = c) ,即c应该分配给bb应该分配给a Rewrite this as a.operator=(b.operator=(c)) . 将其重写为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). 为了使对a的赋值正常工作, b.operator=(c)的返回类型必须引用内部赋值结果(它也可以与copy一起使用,但这只是不必要的开销)。

The dereference operator return type depends on your inner logic, define it in the way that suits your needs. 取消引用运算符的返回类型取决于您的内部逻辑,以适合您需求的方式对其进行定义。

They can both be anything, but usually operator = returns the current object by reference, ie 它们都可以是任何东西,但是通常 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;  

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. 使用此功能,您将能够在链分配中重用该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM