右值如何取消引用?
How is a rvalue dereferenced?
The prefix operators return the object
itself as an lvalue. The postfix operators return a copy of the object’s original value
as an rvalue.
so 在这样的语句中 *a++
a 正在递增并且 a 的原始值的副本作为右值返回,但是来自微软 c++ 语言参考关于左值和右值
An rvalue is a temporary value that does not persist beyond the expression that uses it
并举个例子
// lvalues_and_rvalues1.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main()
{
int x = 3 + 4;
cout << x << endl;
}
In this example, x is an lvalue because it persists beyond the expression that defines it. The expression 3 + 4 is an rvalue because it evaluates to a temporary value that does not persist beyond the expression that defines it.
我的问题:
1) 从 *a++
返回的右值是多少,以便可以取消引用?
2) 我是不是理解错了什么概念?
提前致谢!
*a++
等同于:
auto temp = *a;
a++;
// Use the value of temp here
除非您只能引用该值一次,而 temp
您可以引用多次。
The prefix operators return the object itself as an lvalue. The postfix operators return a copy of the object’s original value as an rvalue.
错了!嗯,主要是。如果引用是在谈论 all prefix/suffix 运算符,那么它就完全错误了。但是,如果它在谈论 ++
和 --
prefix/postfix 对,那么它是正确的。
现在,考虑到这一点...
what is the rvalue returning from the *a++ so that it can be dereferenced?
假设 a
是某种指针,a++
递增 a
并产生一个由 a
递增前的值组成的右值。后缀和前缀形式的递增和递减运算符 ++
和 --
都需要一个左值作为它们的运算符。这是因为右值是临时的,也就是说,它们的范围受它们所在的表达式的限制,因此这些运算符对它们几乎没有意义或没有意义。请记住,这些运算符不仅 inspect/read,而且 change/write to 变量本身。
一元 *
运算符获取一个指针(类似)对象并取消引用它,产生其中的一个左值。它适用于右值和左值指针。这是因为 *
可以被认为是一种 "passive" 运算符。它不会更改/写入指针本身,而是取消引用它和returns指针存储地址处的左值对象,其地址当然是指针所包含的地址。由于 *
所需要的只是包含在指针对象中的内存地址,而指针本身的地址(如果有的话)在这里是无用的,因此 *
对右值和左值都有意义.
你可以认为 *
"requires an rvalue" 和 "lvalues can be used as rvalues when necessary",如果它更能澄清(或混淆?)事情。
The prefix operators return the object itself as an lvalue. The postfix operators return a copy of the object’s original value as an rvalue.
so 在这样的语句中 *a++
a 正在递增并且 a 的原始值的副本作为右值返回,但是来自微软 c++ 语言参考关于左值和右值
An rvalue is a temporary value that does not persist beyond the expression that uses it
并举个例子
// lvalues_and_rvalues1.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main()
{
int x = 3 + 4;
cout << x << endl;
}
In this example, x is an lvalue because it persists beyond the expression that defines it. The expression 3 + 4 is an rvalue because it evaluates to a temporary value that does not persist beyond the expression that defines it.
我的问题:
1) 从 *a++
返回的右值是多少,以便可以取消引用?
2) 我是不是理解错了什么概念?
提前致谢!
*a++
等同于:
auto temp = *a;
a++;
// Use the value of temp here
除非您只能引用该值一次,而 temp
您可以引用多次。
The prefix operators return the object itself as an lvalue. The postfix operators return a copy of the object’s original value as an rvalue.
错了!嗯,主要是。如果引用是在谈论 all prefix/suffix 运算符,那么它就完全错误了。但是,如果它在谈论 ++
和 --
prefix/postfix 对,那么它是正确的。
现在,考虑到这一点...
what is the rvalue returning from the *a++ so that it can be dereferenced?
假设 a
是某种指针,a++
递增 a
并产生一个由 a
递增前的值组成的右值。后缀和前缀形式的递增和递减运算符 ++
和 --
都需要一个左值作为它们的运算符。这是因为右值是临时的,也就是说,它们的范围受它们所在的表达式的限制,因此这些运算符对它们几乎没有意义或没有意义。请记住,这些运算符不仅 inspect/read,而且 change/write to 变量本身。
一元 *
运算符获取一个指针(类似)对象并取消引用它,产生其中的一个左值。它适用于右值和左值指针。这是因为 *
可以被认为是一种 "passive" 运算符。它不会更改/写入指针本身,而是取消引用它和returns指针存储地址处的左值对象,其地址当然是指针所包含的地址。由于 *
所需要的只是包含在指针对象中的内存地址,而指针本身的地址(如果有的话)在这里是无用的,因此 *
对右值和左值都有意义.
你可以认为 *
"requires an rvalue" 和 "lvalues can be used as rvalues when necessary",如果它更能澄清(或混淆?)事情。