为什么rvalues没有地址? [英] Why don't rvalues have an address?
问题描述
为什么rvalues没有内存地址?程序执行时是不是加载到RAM中,或者是指存储在处理器寄存器中的值?
Why don't rvalues have a memory address? Are they not loaded into the RAM when the program executes or does it refer to the values stored in processor registers?
推荐答案
您的问题(为什么rvalues没有内存地址?)有点困惑。价值是一种表达方式。 表达式没有地址:对象具有地址。询问为什么不能将运算符的地址应用于rvalue表达式会更正确?
Your question ("Why don't rvalues have a memory address?") is a bit confused. An rvalue is a kind of expression. Expressions don't have addresses: objects have addresses. It would be more correct to ask "why can one not apply the address-of operator to an rvalue expression?"
答案相当简单:您只能取一个对象的地址,并不是所有的rvalue表达式引用对象(例如,表达式 42
具有一个值,但不引用一个对象)。
The answer to that is rather simple: you can only take the address of an object and not all rvalue expressions refer to objects (for example, the expression 42
has a value but does not refer to an object).
一些rvalue表达式引用对象,但这样的对象缺少持久性。由rvalue表达式引用的对象是一个临时对象,并在创建它的表达式的末尾被销毁。这样的对象确实有地址(您可以通过在临时对象上调用成员函数来轻松发现这个对象; 这个
指针必须指向临时对象,因此临时对象必须有一个地址)。
Some rvalue expressions do refer to objects, but such objects lack persistence. An object referred to by an rvalue expression is a temporary object and is destroyed at the end of the expression in which it is created. Such objects do indeed have addresses (you can easily discover this by calling a member function on a temporary object; the this
pointer must point to the temporary object and thus the temporary object must have an address).
这是lvalue表达式和rvalue表达式之间的根本区别。 Lvalue表达式是指具有持久性的对象:一个lvalue表达式所指向的对象超越单个表达式。
This is the fundamental difference between lvalue expressions and rvalue expressions. Lvalue expressions refer to objects that have persistence: the object to which an lvalue expression refers persists beyond a single expression.
这篇关于为什么rvalues没有地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!