1.写出判断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值(3分)
int a = 4;
(A)a += (a++); (B) a += (++a) ;(C) (a++) += a;(D) (++a) += (a++);
a = ?
答:C错误,左侧不是一个有效变量,不能赋值,可改为(++a) += a;
no need to be sorry, you made a good point. :-)
constant objects do have names, but can't be used as lvalues. that is pretty clear.
the reason for that i said '++a' had a name is because '++a' is equivalent to this: 'a = a + 1; a;' i.e. the final result is 'a', which has incremented by 1; on the contrary, 'a++' means this: 'tmp = a; a = a + 1; tmp;', i.e. the final result is a temporary object, which is equal to the 'a' before incrementation. the temporary doesn't have a name, so it can't be a lvalue.
if your question is about how to judge whether a value can be used as a lvalue, that is a good rule of thumb (not necessarily be the lvalue's definition though), i.e. all named objects (excluding constants, and objects of some special user-defined classes (they can be made un-assignable) surely) can be used as lvalues, unnamed objects can not.