关于左值的问题
这样是有问题的d>0?(c+=1):(d?a+=1:b+=1);
这样是没问题的if(d>0)
c+=1;
else if(d)
a+=1;
else
b+=1;
不理解为什么,请大神给好好讲一讲有关左值的问题
d>0?(c+=1):(d?a+=1:**_(_**b+=1**_)_**);
我试了没问题的啊
应该没有问题啊。你用的什么编译器?
d>0?(c+=1):(d?a+=1:b+=1);
如果d > 0;的话, 冒号后面的表达式即(d ? a +=1 : b+=1)不用计算了。
你的意思指的是这个吗?
是不是 ?:表达式里:左右的表达式的地位是不一样的?左边允许是左值,右边不允许?
不清楚为什么,同问。
For example, in C, the syntax for a conditional expression is:
logical-OR-expression ? expression : conditional-expression
while in C++ it is:
logical-OR-expression ? expression : assignment-expression
Hence, the expression:
e = a < d ? a++ : a = d
is parsed differently in the two languages. In C, this expression is a syntax error, but many compilers parse it as:
e = ((a < d ? a++ : a) = d)
which is a semantic error, since th
e result of the conditional-expression (which might be a++) is not an lvalue. In C++, it is parsed as:
e = (a < d ? a++ : (a = d))
which is a valid expression.