由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问个c++的弱问题
相关主题
关于 expression template 这种叫法auto 很爽
也问个regular expression的问题《新C++标准:C++0x 概述》英文文字版[PDF]
a C++ questionMicrosoft Visual C++ 2010 Express
Really Stupid Question: How to run VC++ 2008 Express (转载)开始学python,要被它打败了
which Regular Expression lib in C++ do you prefer?感觉JVM上的FP语言都有个大问题
Any possibility to make this expression faster?Go 语言的综合评论 by 王垠
[合集] 统计,Regular Expressionclojure能单独做大系统么?
C++ question最expressive的三大语言
相关话题的讨论汇总
话题: reference话题: returning话题: operator话题: assignment话题: class
进入Programming版参与讨论
1 (共1页)
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:

1 (共1页)
进入Programming版参与讨论
相关主题
最expressive的三大语言which Regular Expression lib in C++ do you prefer?
TIOBE Index August Headline: Java stronger in the leadAny possibility to make this expression faster?
几年前一个科学预言python的崛起[合集] 统计,Regular Expression
有没有给C用的regular expression的库?C++ question
关于 expression template 这种叫法auto 很爽
也问个regular expression的问题《新C++标准:C++0x 概述》英文文字版[PDF]
a C++ questionMicrosoft Visual C++ 2010 Express
Really Stupid Question: How to run VC++ 2008 Express (转载)开始学python,要被它打败了
相关话题的讨论汇总
话题: reference话题: returning话题: operator话题: assignment话题: class