增量操作的不可预测结果[英] Unpredictable result of Increment operation

本文是小编为大家收集整理的关于增量操作的不可预测结果的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到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
因为这是荒谬的未定义行为.

为什么没有意义?为什么未定义?
你是对的,两者都不是.抱歉,我看到的帖子太多了
是和做...

--
伊恩·柯林斯.

本文地址:https://www.itbaoku.cn/post/1050849.html

问题描述

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 ?

int i;

i=1; cout << (++i)++; Output: 2
Because it''s nonsensical undefined behaviour.

--
Ian Collins.

Ian Collins wrote:
bintom wrote:
>Is there any reason why the following C++ code behaves as it does ?

int i;

i=1; cout << (++i)++; Output: 2
Because it''s nonsensical undefined behaviour.
Why is it non-sensical? Why is it undefined?

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:
>bintom wrote:
>>Is there any reason why the following C++ code behaves as it does ?

int i;

i=1; cout << (++i)++; Output: 2
Because it''s nonsensical undefined behaviour.

Why is it non-sensical? Why is it undefined?
You are right, it is neither. Sorry, I''ve seen way too many posts that
are and do...

--
Ian Collins.