C ++,增量运算符 [英] C++, increment operator

查看:93
本文介绍了C ++,增量运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int k, n = 2;
k = (k = n + 5)++;
cout << k << '\n';
k = ++(k = n + 5);
cout << k << '\n';



我以为第一个是7,第二个是8.但是令人惊讶的是,输出都是8.
有人可以告诉我吗?



I thought the 1st one is 7 and the 2nd is 8. But the output is surprisingly both 8.
Anyone can tell me about this? Thx in advance.

推荐答案

在C ++中,它未定义为i = i++,因为i在同一表达式中被修改了两次,并且顺序未定义为operator=不是序列点.

请参见 http://en.wikipedia.org/wiki/Sequence_point [
In C++, it is undefined as is i = i++ since i is modified twice in the same expression and the order is not defined as operator= is not a sequence point.

See http://en.wikipedia.org/wiki/Sequence_point[^]


第一个语句也返回8,因为
当该语句的执行完成
The 1st statement also returning 8 because
when the execution of this statement is completed
k = (k = n + 5)++;

时,也会进行后递增,并且得到的结果为8.

如果打印(k = n + 5)++,则输出为7.

then the postincrement also takes place and you are getting result as 8.

if you print (k = n + 5)++ then the output will be 7.


这篇关于C ++,增量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆