S*******s 发帖数: 13043 | 1 a typical copy assignment operator declaration is like following:
A & operator= (const A& other);
why its return type is non-const, instead of like following:
const A & operator= (const A& other);
will there be situantion that the operator be the L value, like
(a = b) = c ? | X****r 发帖数: 3557 | 2 Because in C++, the (un-overloaded) assignment operator returns a
the value of its left operand, which is a l-value, see C++98 5.17
[expr.ass].
【在 S*******s 的大作中提到】 : a typical copy assignment operator declaration is like following: : A & operator= (const A& other); : why its return type is non-const, instead of like following: : const A & operator= (const A& other); : will there be situantion that the operator be the L value, like : (a = b) = c ?
| S*******s 发帖数: 13043 | 3 why is it designed like that? what advantage compared to returning const?
【在 X****r 的大作中提到】 : Because in C++, the (un-overloaded) assignment operator returns a : the value of its left operand, which is a l-value, see C++98 5.17 : [expr.ass].
| a***y 发帖数: 2803 | 4 你知道const的意思是什么吗?const就是说这个地址的内容不能更改.
注意,返回的不是一个值,而是一个reference,*this.*this意味着当前object.因为当前
object的值可以任意更改,所以返回值是A&,而不是const A&.
【在 S*******s 的大作中提到】 : a typical copy assignment operator declaration is like following: : A & operator= (const A& other); : why its return type is non-const, instead of like following: : const A & operator= (const A& other); : will there be situantion that the operator be the L value, like : (a = b) = c ?
| a***y 发帖数: 2803 | 5 it's called assignment operator,not copy assignment operator.
【在 S*******s 的大作中提到】 : a typical copy assignment operator declaration is like following: : A & operator= (const A& other); : why its return type is non-const, instead of like following: : const A & operator= (const A& other); : will there be situantion that the operator be the L value, like : (a = b) = c ?
| S*******s 发帖数: 13043 | 6 no need to use arrogant tone. anyway, can you give an example that const
reference return type will fail? do you understand my last question in the OP?
【在 a***y 的大作中提到】 : 你知道const的意思是什么吗?const就是说这个地址的内容不能更改. : 注意,返回的不是一个值,而是一个reference,*this.*this意味着当前object.因为当前 : object的值可以任意更改,所以返回值是A&,而不是const A&.
| S*******s 发帖数: 13043 | 7 why not google "copy assignment operator" b4 posting?
【在 a***y 的大作中提到】 : it's called assignment operator,not copy assignment operator.
| t****t 发帖数: 6806 | 8 yes i understand your question. xentar answered it, let me rephrase: for
built-in types, (a=b)=c is legitimate (from syntax point of view. it is
undefined though), so overloaded operators mimic it. why (a=b)=c is
legitimate from the beginning? because it is legitimate in C.
OP?
【在 S*******s 的大作中提到】 : no need to use arrogant tone. anyway, can you give an example that const : reference return type will fail? do you understand my last question in the OP?
| a***y 发帖数: 2803 | 9 (a=b)=c 的目的是什么?
【在 S*******s 的大作中提到】 : a typical copy assignment operator declaration is like following: : A & operator= (const A& other); : why its return type is non-const, instead of like following: : const A & operator= (const A& other); : will there be situantion that the operator be the L value, like : (a = b) = c ?
| S*******s 发帖数: 13043 | 10 if (a=b)=c is legitimate, b stays its original value, instead of c,
contradicting to ppl's reasonable expectation.
it seems a bad design.
【在 t****t 的大作中提到】 : yes i understand your question. xentar answered it, let me rephrase: for : built-in types, (a=b)=c is legitimate (from syntax point of view. it is : undefined though), so overloaded operators mimic it. why (a=b)=c is : legitimate from the beginning? because it is legitimate in C. : : OP?
| | | a***y 发帖数: 2803 | 11 (a=b)=c 和 a=b有什么区别?
【在 S*******s 的大作中提到】 : if (a=b)=c is legitimate, b stays its original value, instead of c, : contradicting to ppl's reasonable expectation. : it seems a bad design.
| S*********g 发帖数: 5298 | 12 如果b是auto_ptr,那么这个(a=b)之后,b就没了
【在 S*******s 的大作中提到】 : if (a=b)=c is legitimate, b stays its original value, instead of c, : contradicting to ppl's reasonable expectation. : it seems a bad design.
| S*******s 发帖数: 13043 | 13 it has nothing to do with the topic here ba.
【在 S*********g 的大作中提到】 : 如果b是auto_ptr,那么这个(a=b)之后,b就没了
| t****t 发帖数: 6806 | 14 i dunno what's your reasonable expectation, but for C users, a=b=c means a=(
b=c), and that's the usual chain assignment. of course (a=b)=c is unusual,
and shouldn't be used because it's undefined for built-in types anyway. for
mimicing built-in type's purpose, it shouldn't be used for class either. no
one said it's a good design.
【在 S*******s 的大作中提到】 : if (a=b)=c is legitimate, b stays its original value, instead of c, : contradicting to ppl's reasonable expectation. : it seems a bad design.
| S*******s 发帖数: 13043 | 15 I think the intuitive expected result after ((a=b)=c ) is that all three
variables shares same value.if we define the return type const, we can
prevent such usage.
but current non-const declaration failed to do so. Thus I called it bad
design.
=(
for
no
【在 t****t 的大作中提到】 : i dunno what's your reasonable expectation, but for C users, a=b=c means a=( : b=c), and that's the usual chain assignment. of course (a=b)=c is unusual, : and shouldn't be used because it's undefined for built-in types anyway. for : mimicing built-in type's purpose, it shouldn't be used for class either. no : one said it's a good design.
| a***y 发帖数: 2803 | 16 对,操作符 = 的执行顺序是从右向左.
=(
for
no
【在 t****t 的大作中提到】 : i dunno what's your reasonable expectation, but for C users, a=b=c means a=( : b=c), and that's the usual chain assignment. of course (a=b)=c is unusual, : and shouldn't be used because it's undefined for built-in types anyway. for : mimicing built-in type's purpose, it shouldn't be used for class either. no : one said it's a good design.
| S*******s 发帖数: 13043 | 17 your first reply said (a=b)=c is legimate in C. but you now said it is
undefined for built-in types. so can you explain further?
=(
for
no
【在 t****t 的大作中提到】 : i dunno what's your reasonable expectation, but for C users, a=b=c means a=( : b=c), and that's the usual chain assignment. of course (a=b)=c is unusual, : and shouldn't be used because it's undefined for built-in types anyway. for : mimicing built-in type's purpose, it shouldn't be used for class either. no : one said it's a good design.
| j*******e 发帖数: 674 | 18 C++ const is more of a "logical constant", but not "bit by bit constant".
you can certainly return a const reference for the assignment opreator, no
matter it is "*this" or "const *this".
【在 a***y 的大作中提到】 : 你知道const的意思是什么吗?const就是说这个地址的内容不能更改. : 注意,返回的不是一个值,而是一个reference,*this.*this意味着当前object.因为当前 : object的值可以任意更改,所以返回值是A&,而不是const A&.
| a***y 发帖数: 2803 | 19 那你是说这个是对的?
const A & operator= (const A& other);
【在 j*******e 的大作中提到】 : C++ const is more of a "logical constant", but not "bit by bit constant". : you can certainly return a const reference for the assignment opreator, no : matter it is "*this" or "const *this".
| t****t 发帖数: 6806 | 20 in my first reply i said it is legitimate from syntax point of view. which m
eans it can compile. but compile and undefined are not mutually exclusive. e
.g. "a[i++]=i" is legitimate from syntax point of view, but undefined.
it is undefined because in (a=b)=c, a changed value twice. this is undefined
.
【在 S*******s 的大作中提到】 : your first reply said (a=b)=c is legimate in C. but you now said it is : undefined for built-in types. so can you explain further? : : =( : for : no
| | | t****t 发帖数: 6806 | 21 yes you can declare like this. then (a=b)=c will fail to compile. but some p
pl think overloaded operators should mimic built-in type, which includes (a=
b)=c. in fact, as a convention, all STL class returns (non-const) reference
to *this in assignment operator. scott meyers mentioned this in
++>.
【在 a***y 的大作中提到】 : 那你是说这个是对的? : const A & operator= (const A& other);
| t****t 发帖数: 6806 | 22 there is NO "execution order" defined here. for C/C++ operators, you may say
precedence and associativity, which defines the binding order of operators:
A op1 B op2 C
is parsed as
(A op1 B) op2 C
or
A op1 (B op2 C)
if op1 has high precedence, then 1st form is taken; op2 has higher precedenc
e, 2nd form is taken. if op1 and op2 has same precedence, associativity kick
s in: if they have left assoc., 1st form is taken, otherwise 2nd form is ta
ken.
however, "execution order" is subtle. in 1st form, op2 depends on result of
op1, we expect op1 "executes" first. but C/C++ operators do two things: (1)
it returns a value, which is indeed "executed" in the order of dependency (i
f there is any). (2) it may have side effect, such as changing the value of
an object, which includes the effect of all assignment operators and ++, --.
the order of side effect is not defined (not defined doesn't mean undefined)
between 2 sequence points. so you may only change the value of an
object at most ONCE between any 2
sequence points. so (a=b)=c means a=b and a=c in an unspecified order, which
ultimately leads to undefined behaviour.
unfortunately, when c/c++ novice say "execution order" (in an expression),
usually they really meant "execution order of side effects", which does not
exist at all.
【在 a***y 的大作中提到】 : 对,操作符 = 的执行顺序是从右向左. : : =( : for : no
| a***y 发帖数: 2803 | 23 #include
using namespace std;
int main() {
int a[5];
a[0]=9;
a[1]=8;
a[2]=7;
a[3]=6;
a[4]='\0';
int i=1;
a[i++]=i;
a[i]=i++;
cout << "a[0] is "<< a[0] << endl;
cout << "a[1] is "<< a[1] << endl;
cout << "a[2] is "<< a[2] << endl;
return 0;
}
运行结果是
a[0] is 9
a[1] is 1
a[2] is 2
a[i++]=i;语法正确,而且运行也正确啊.
m
e
undefined
【在 t****t 的大作中提到】 : in my first reply i said it is legitimate from syntax point of view. which m : eans it can compile. but compile and undefined are not mutually exclusive. e : .g. "a[i++]=i" is legitimate from syntax point of view, but undefined. : it is undefined because in (a=b)=c, a changed value twice. this is undefined : .
| a***y 发帖数: 2803 | 24 #include
using namespace std;
int main() {
int a[5];
a[0]=9;
a[1]=8;
a[2]=7;
a[3]=6;
a[4]='\0';
int i=1;
a[i++]=i;
a[i]=i++;
(a[3]=4)=5;
cout << "a[0] is "<< a[0] << endl;
cout << "a[1] is "<< a[1] << endl;
cout << "a[2] is "<< a[2] << endl;
cout << "a[3] is "<< a[3] << endl;
return 0;
}
结果是
a[0] is 9
a[1] is 1
a[2] is 2
a[3] is 5
(a[3]=4)=5;最后a[3]等于5,不是4. 语法正确,结果正确.
say
operators:
precedenc
kick
ta
【在 t****t 的大作中提到】 : there is NO "execution order" defined here. for C/C++ operators, you may say : precedence and associativity, which defines the binding order of operators: : A op1 B op2 C : is parsed as : (A op1 B) op2 C : or : A op1 (B op2 C) : if op1 has high precedence, then 1st form is taken; op2 has higher precedenc : e, 2nd form is taken. if op1 and op2 has same precedence, associativity kick : s in: if they have left assoc., 1st form is taken, otherwise 2nd form is ta
| a***y 发帖数: 2803 | 25 假设a,b,c都是int.
(a=b)=c 就是
1.括号里的运算优先,a=b,然后这个int& operator=(int& ) { }的返回值是int&,因为
是一个reference,其实返回的是变量a,而不是变量a的值,这个区别很重要.
2.返回的reference int& a = c,这样就是变量a的值变为了c的值.
所以,最后a变为c的值.
【在 S*******s 的大作中提到】 : a typical copy assignment operator declaration is like following: : A & operator= (const A& other); : why its return type is non-const, instead of like following: : const A & operator= (const A& other); : will there be situantion that the operator be the L value, like : (a = b) = c ?
| t****t 发帖数: 6806 | 26 "undefined" means anything can happen, including the result you wanted.
running "correct" doesn't mean anything.
【在 a***y 的大作中提到】 : #include : using namespace std; : int main() { : int a[5]; : a[0]=9; : a[1]=8; : a[2]=7; : a[3]=6; : a[4]='\0'; : int i=1;
| a***y 发帖数: 2803 | 27 编译器处理含糊的运算时有些规律,比如
i+++j
编译器就认为是(i++)+j,而不是i+(++j)
【在 t****t 的大作中提到】 : "undefined" means anything can happen, including the result you wanted. : running "correct" doesn't mean anything.
| S*******s 发帖数: 13043 | 28 there are quite a lot compilors in this world. none of them bug free or 100%
comply to the standard. you should not rely on the result of a specific
compilor to understand the language because you code could run ok today but
corrupt tomorrow.
【在 a***y 的大作中提到】 : 编译器处理含糊的运算时有些规律,比如 : i+++j : 编译器就认为是(i++)+j,而不是i+(++j)
| t****t 发帖数: 6806 | 29 i have been through this a lot of times, i don't want to do it again. go
read C FAQ if you have time.
【在 a***y 的大作中提到】 : 编译器处理含糊的运算时有些规律,比如 : i+++j : 编译器就认为是(i++)+j,而不是i+(++j)
| S*******s 发帖数: 13043 | | | | t****t 发帖数: 6806 | 31 you didn't read my post, did you? i said "execute" is a vague word and there
is no "execution order" here.
【在 a***y 的大作中提到】 : 假设a,b,c都是int. : (a=b)=c 就是 : 1.括号里的运算优先,a=b,然后这个int& operator=(int& ) { }的返回值是int&,因为 : 是一个reference,其实返回的是变量a,而不是变量a的值,这个区别很重要. : 2.返回的reference int& a = c,这样就是变量a的值变为了c的值. : 所以,最后a变为c的值.
|
|