后缀增量 [英] Postfix increment

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

问题描述

您好!


我发现有一个有趣的事情是使用后缀运算符++。


什么应该包含变量i以下代码:


int i = 5;

i = i ++;


在VC ++ 7.1和VC ++ 2005 beta中2变量i包含6.

但在C#7.1和C#2005 beta 2中变量i包含5.


行为是否正确?谁能解释一下结果是什么呢?


祝你好运,

Oleg。

解决方案

你好


嗯,看起来很奇怪。我已经在C ++和C#(VS 2003)上进行了测试,并且

得到了不同的结果。


我期待在C#中得到6,但是给了我5!


所以,看起来我们有下一个算法:

t = i; //一些临时变量或寄存器

i ++; //这里我们得到i = 6;

i = t; //这里我们回来了i = 5


很奇怪!!!

-

最好的问候,

Andrew

http://www.codeproject.com/script/pr...asp?id=1181072

" Oleg Kharitonov" < KH ***** @ mail.ru>在消息中写道

news:OR ************* @ tk2msftngp13.phx.gbl ...

你好!

我用postfix operator ++找到了一个有趣的东西。

在得到以下代码之后应该包含变量i:

int i = 5; <在VC ++ 7.1和VC ++ 2005 beta 2中,变量i包含6.
但在C#7.1和C#2005 beta 2中,变量i包含5个。
i = i ++;

/>
行为是否正确?谁能解释一下这是什么结果呢?

最好的问候,
Oleg。



有趣的评论:)


但我对你的评论有疑问...... VC ++有托管或非托管

代码?我可以推测未受管理的情况......


" Oleg Kharitonov" < KH ***** @ mail.ru> schrieb im Newsbeitrag

新闻:或者************* @ tk2msftngp13.phx.gbl ...

你好!

我用postfix operator ++找到了一个有趣的东西。

在得到以下代码之后应该包含变量i:

int i = 5; <在VC ++ 7.1和VC ++ 2005 beta 2中,变量i包含6.
但在C#7.1和C#2005 beta 2中,变量i包含5个。
i = i ++;

/>
行为是否正确?谁能解释什么是正确的结果?

最好的问候,
Oleg。



C#的行为是正确的,C ++的行为是不正确。

来自C ++参考:

重要的是要注意后缀增量或减量表达式

计算前面的表达式的值应用

各自的运营商。


来自C#的关于后缀增量的参考:

操作的结果是值之前的操作数是

递增。


因此示例中i ++的返回值是5而不是6.


Hello!

I have found an interesting thing with postfix operator ++.

What should contain the variable i after exceution the following code:

int i = 5;
i = i++;

In VC++ 7.1 and VC++ 2005 beta 2 the variable i contains 6.
But in C# 7.1 and C# 2005 beta 2 the variable i contains 5.

Is it behaviour correct? Can anyone explain what is the correct result?

Best regards,
Oleg.

解决方案

Hello

Hmmm, it looks really strange. I''ve tested it on C++ and C# (VS 2003) and
got different result.

I was expecting to get 6 in C#, but gave me 5 !

So, it looks like it we have the next algorithm:
t = i; // some temp variable or register
i++; // here we get i=6;
i=t; // here we get back i=5

Very strange !!!
--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"Oleg Kharitonov" <kh*****@mail.ru> wrote in message
news:OR*************@tk2msftngp13.phx.gbl...

Hello!

I have found an interesting thing with postfix operator ++.

What should contain the variable i after exceution the following code:

int i = 5;
i = i++;

In VC++ 7.1 and VC++ 2005 beta 2 the variable i contains 6.
But in C# 7.1 and C# 2005 beta 2 the variable i contains 5.

Is it behaviour correct? Can anyone explain what is the correct result?

Best regards,
Oleg.



Interesting remark :)

But I have a question on your remark ... VC++ with managed or unmanaged
code ? I can speculate on the unmanaged situation ...


"Oleg Kharitonov" <kh*****@mail.ru> schrieb im Newsbeitrag
news:OR*************@tk2msftngp13.phx.gbl...

Hello!

I have found an interesting thing with postfix operator ++.

What should contain the variable i after exceution the following code:

int i = 5;
i = i++;

In VC++ 7.1 and VC++ 2005 beta 2 the variable i contains 6.
But in C# 7.1 and C# 2005 beta 2 the variable i contains 5.

Is it behaviour correct? Can anyone explain what is the correct result?

Best regards,
Oleg.


The behavior of C# is correct, the behavior of C++ is incorrect.
From C++ reference:
It is important to note that a postfix increment or decrement expression
evaluates to the value of the expression prior to application of the
respective operator.

From C# reference about postfix increment:
The result of the operation is the value of the operand before it has been
incremented.

So the return value of i++ in the example is 5 not 6.


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

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