本文是小编为大家收集整理的关于增量操作的不可预测结果的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
以下 C++ 代码的行为是否有任何原因?
诠释我;
我=1;cout <<(++i)++;输出:2
我=1;cout <<++(++i);输出:3
我=1;cout <<(i++)++;输出:错误.需要左值
我=1;cout <<++(i++);输出:错误.需要 LValue
推荐答案
bintom 写道:以下 C++ 代码的行为是否有任何原因?因为这是荒谬的未定义行为.
诠释我;
我=1;cout <<(++i)++;输出:2
--
伊恩·柯林斯.
伊恩·柯林斯写道:bintom 写道:为什么没有意义?为什么是未定义的?>下面的 C++ 代码的行为有什么原因吗?因为这是荒谬的未定义行为.
int i;
i=1;cout <<(++i)++;输出:2
五
--
邮件回复时请去掉大写的"A"
我不回复置顶的回复,请不要问
Victor Bazarov 写道:伊恩·柯林斯写道:你是对的,两者都不是.抱歉,我看到的帖子太多了>bintom 写道:>>以下 C++ 代码的行为是否有任何原因?因为这是荒谬的未定义行为.
int i;
i=1;cout <<(++i)++;输出:2
为什么没有意义?为什么未定义?
是和做...
--
伊恩·柯林斯.
问题描述
Is there any reason why the following C++ code behaves as it does ?
int i;
i=1; cout << (++i)++; Output: 2
i=1; cout << ++(++i); Output: 3
i=1; cout << (i++)++; Output: Error. LValue required
i=1; cout << ++(i++); Output: Error. LValue required
推荐答案
bintom wrote:Is there any reason why the following C++ code behaves as it does ?Because it''s nonsensical undefined behaviour.
int i;
i=1; cout << (++i)++; Output: 2
--
Ian Collins.
Ian Collins wrote:bintom wrote:Why is it non-sensical? Why is it undefined?>Is there any reason why the following C++ code behaves as it does ?Because it''s nonsensical undefined behaviour.
int i;
i=1; cout << (++i)++; Output: 2
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Victor Bazarov wrote:Ian Collins wrote:You are right, it is neither. Sorry, I''ve seen way too many posts that>bintom wrote:>>Is there any reason why the following C++ code behaves as it does ?Because it''s nonsensical undefined behaviour.
int i;
i=1; cout << (++i)++; Output: 2
Why is it non-sensical? Why is it undefined?
are and do...
--
Ian Collins.