(++ i)和(i ++)之间的区别 [英] Difference between (++i) and (i++)

查看:90
本文介绍了(++ i)和(i ++)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,我理解(++i)应该返回对i的引用,因为需要连接运算符,但是我不知道的是:

In C++ I understand that (++i) should return a reference to i because the need of concatenation of operators, but what I can't figure out is:

为什么(i++)应该按值返回i?

任何人都可以澄清一下.

Can anyone please clarify.

推荐答案

++i可以写为

prefix_inc (this) {
   increase this by 1
   return this
}

由于返回了真实的i,因此我们可以对其进行引用.但是,i++看起来像

Since the real i is returned, we can take reference of it. However, i++ looks like

postfix_inc (this) {
   set old_this = copy of this
   increase this by 1
   return old_this
}

因为old_this只是局部变量,所以在i++完成后对其的引用是没有意义的.因此,从逻辑上讲,它应该返回一个右值.

as old_this is just a local variable, the reference of it is pointless after i++ is completed. So logically it should return an rvalue.

这篇关于(++ i)和(i ++)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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