为什么++ x是左值而x ++是右值? [英] Why is ++x a lvalue and x++ a rvalue?

查看:307
本文介绍了为什么++ x是左值而x ++是右值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在阅读左值和右值,对于 ++ x x ++ 进行此分类。

So I've been reading up on lvalue and rvalues and I'm a bit confused about the difference between ++x and x++ when it comes to this categorization.

为什么 ++ x 是左值,而 x ++ 一个右值?

Why is ++x a lvalue and x++ a rvalue?

推荐答案

++ x 返回对该对象的引用您增加了,其中 x ++ 返回 x 的旧值的临时副本。

++x returns a reference to the object you incremented, where as x++ returns a temporary copy of x's old value.

至少这是按惯例实现这些运算符的正常方式。并且所有内置类型都以这种方式工作。而且,如果您已经了解了左值/右值,那么您会发现,由于前缀运算符本身返回命名对象,因此它将是左值,而后缀运算符将返回本地临时副本,然后该副本将作为右值

At least this would be the "normal" way by to implement these operators by convention. And all built-in types work this way. And if you've read about lvalues / rvalues then you would see that since the prefix operator returns the named object itself it would be an lvalue, where as the postfix operator returns a copy of a local temporary, which would then qualify as an rvalue.

注意:另外,我们现在有prvalues,xvalues等,因此这些天在技术上要复杂一些。请在此处获取更多信息。

这篇关于为什么++ x是左值而x ++是右值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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