简体   繁体   English

& 返回左值或右值是什么?

[英]What does & return a lvalue or a rvalue?

As per textbook.按照教科书。

The address-of operator (§ 2.3.2, p. 52) requires an lvalue operand and returns a pointer to its operand as an rvalue.地址运算符(第 2.3.2 节,第 52 页)需要一个左值操作数,并将指向其操作数的指针作为右值返回。

int a = 6,c=9;
int *x = &a; //Here x is a lvalue.
x = &c;

As per my knowledge if we can assign, it is lvalue.Then why does textbook says address of return rvalue.Can anyone explain me this in layman language据我所知,如果我们可以分配,它是左值。那么为什么教科书上说返回右值的地址。谁能用外行语言解释一下

Text book says &a is an rvalue.教科书说&a是一个右值。 Ie value cannot be assigned to &a .即值不能分配给&a Compilation error will occur if we try to compile below code.如果我们尝试编译下面的代码,将会发生编译错误。

int a = 6,c=9;
int *x = &a; //Here x is a lvalue.
&a = x; // Compilation error, as '&a' is an rvalue

As per my knowledge if we can assign据我所知,如果我们可以分配

This is not necessarily true in case of class types.在 class 类型的情况下,这不一定是正确的。 But it does always hold for pointers, which you use in the example.但它始终适用于您在示例中使用的指针。

Then why does textbook says address of return rvalue那为什么教科书说返回右值的地址

Well, you cannot assign to it:好吧,你不能分配给它:

&a = &c; // ill formed because &a is rvalue
int *x = &a; // OK. Not an assignment
x = &c;  // OK because x is lvalue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM