l*****d 发帖数: 359 | 1 why copy assignment member functions need to return a reference to the class
instead of the class itself?
i know for other operator overloading such as +-*/, returning a reference
can lead to allowing of nonsense expressions like (a+b)=c, thus returning a
class itself is preferred; and for [], returning a reference is
preferred so that a[i]=b is allowed.
However for operator=, it seems to me that returning a class is ok because x
=y=z behaves like x=(y=z), not (x=y)=z | l*****d 发帖数: 359 | 2 ok, i found the answer, because some times crazy stuff like (x=y)=z is
needed... weird | t****t 发帖数: 6806 | 3 no, that's not the answer to your question
the answer to your question is, what's the point to make a copy of *this?
your answer is to the following question: why operator= doesn't return const
T&?
【在 l*****d 的大作中提到】 : ok, i found the answer, because some times crazy stuff like (x=y)=z is : needed... weird
| l*****d 发帖数: 359 | 4 my original intent is to prevent crazy stuff like (x=y)=z, just like operator +-*/ returns a class instead of a reference to class
but now it seems to be not that crazy
const
【在 t****t 的大作中提到】 : no, that's not the answer to your question : the answer to your question is, what's the point to make a copy of *this? : your answer is to the following question: why operator= doesn't return const : T&?
| t****t 发帖数: 6806 | 5 as i said, to prevent that, you write
const T& T::operator=(...)
【在 l*****d 的大作中提到】 : my original intent is to prevent crazy stuff like (x=y)=z, just like operator +-*/ returns a class instead of a reference to class : but now it seems to be not that crazy : : const
| l*****d 发帖数: 359 | 6 ok, so it's for efficiency
【在 t****t 的大作中提到】 : as i said, to prevent that, you write : const T& T::operator=(...)
| T*****9 发帖数: 2484 | 7 是不是因为C++本身的缺陷,她不能有效的返回一个类对象?
【在 t****t 的大作中提到】 : as i said, to prevent that, you write : const T& T::operator=(...)
| T*****9 发帖数: 2484 | 8 是不是因为这个?
Assignment Should Return a Reference to *this
The string assignment operators return a reference to string, which is
consistent with assignment for the built-in types. Moreover, because
assignment returns a reference there is no need to create and destroy a
temporary copy of the result. The return value is usually a reference to the
left-hand operand. For example, here is the definition of the Sales_item
compound-assignment operator:
【在 t****t 的大作中提到】 : as i said, to prevent that, you write : const T& T::operator=(...)
| l*****d 发帖数: 359 | 9 thanks. so even built-in types return references
the
【在 T*****9 的大作中提到】 : 是不是因为这个? : Assignment Should Return a Reference to *this : The string assignment operators return a reference to string, which is : consistent with assignment for the built-in types. Moreover, because : assignment returns a reference there is no need to create and destroy a : temporary copy of the result. The return value is usually a reference to the : left-hand operand. For example, here is the definition of the Sales_item : compound-assignment operator:
|
|