Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 请在此澄清左值和右值的概念_C++_C - Fatal编程技术网

C++ 请在此澄清左值和右值的概念

C++ 请在此澄清左值和右值的概念,c++,c,C++,C,我有下面给出的程序 #include<stdio.h> int main() { int i=5; (++(++i)); } #包括 int main() { int i=5; (++(++i)); } 这个程序编译C++中的精细而不是C。我也不能真正理解。但是我尝试了阅读和搜索,发现这是因为PrimePosiver操作符返回C中的rValue+C++中的LValue/ 如果将(++(++)i) > (++(i++))< /> >,则编译在C和C++中都失败,因为PO

我有下面给出的程序

#include<stdio.h>
int main()
{
   int i=5;
   (++(++i));
}
#包括
int main()
{
int i=5;
(++(++i));
}
这个程序编译C++中的精细而不是C。我也不能真正理解。但是我尝试了阅读和搜索,发现这是因为PrimePosiver操作符返回C中的rValue+C++中的LValue/

如果将<代码>(++(++)i)<代码> > <代码>(++(i++))< /> >,则编译在C和C++中都失败,因为POST增量总是返回rVal.< /P>


即使经过一些阅读,我也不清楚左值和右值在这里的确切含义。有人能用外行的术语解释一下这些是什么吗?

右值本质上是原始值或操作的临时结果,不应该对它们进行操作。因此,不允许对这些对象执行增量前或增量后操作

左值是指存储的对象、函数或原语的传统值,可以对其进行操作或调用

在第一行:
inti=5;//i是左值,5是右值

因此,
++(i++)
正在转换为
++6
,这正是编译器所抱怨的


顺便说一句,这里已经给出了答案:

右值本质上是原始值或操作的临时结果,不应该对它们进行操作。因此,不允许对这些对象执行增量前或增量后操作

左值是指存储的对象、函数或原语的传统值,可以对其进行操作或调用

在第一行:
inti=5;//i是左值,5是右值

因此,
++(i++)
正在转换为
++6
,这正是编译器所抱怨的

顺便说一句,这里已经回答了这个问题:

“左值(定位器值)表示在内存中占据某个可识别位置(即具有地址)的对象

rvalues是通过排除定义的,它表示每个表达式都是左值或rvalue。因此,从上述lvalue定义中,rvalue是一个表达式,它不表示占用内存中某个可识别位置的对象。”

参考:

根据这个定义,, 增量后的
i++
将不再有效,因为返回的表达式在增量后不再位于
i

同时,由
++i
返回的表达式返回对递增变量
i
的引用

“左值(定位器值)表示在内存中占据某个可识别位置(即具有地址)的对象

R值是通过排除来定义的,表示每个表达式要么是左值,要么是右值。因此,根据上述左值定义,R值是一个表达式,它不表示占用内存中某个可识别位置的对象。”

参考:

根据这个定义,, 增量后的
i++
将不再有效,因为返回的表达式在增量后不再位于
i


同时,由
++i
返回的表达式返回对c中递增变量
i

的引用。后缀或前缀
++
运算符要求操作数是可修改的左值。两个操作符都执行左值转换,因此对象不再是左值

C++还要求前缀
++
运算符的操作数是可修改的左值,但前缀
++
运算符的结果是左值。后缀
++
运算符的情况并非如此


因此
(++(++i))在第二个操作获得左值时编译,但
(++(i++)
不编译。

在c中,后缀或前缀
++
运算符要求操作数是可修改的左值。两个操作符都执行左值转换,因此对象不再是左值

"lvalue" and "rvalue" are so named because of where each 
  of them can appear in an assignment operation.  An 
  lvalue 
 can appear on the left side of an assignment operator, 
  whereas an rvalue can appear on the right side.

  As an example:

int a;
a = 3;

In the second line, "a" is the lvalue, and "3" is the rvalue.

 in this example:



int a, b;
a = 4;
b = a;

In the third line of that example, "b" is the lvalue, and "a" is 
the rvalue, whereas it was the lvalue in line 2.  This 
illustrates an important point: An lvalue can also be an 
rvalue, but an rvalue can never be an lvalue.

Another definition of lvalue is "a place where a value can 
be stored." 
C++还要求前缀
++
运算符的操作数是可修改的左值,但前缀
++
运算符的结果是左值。后缀
++
运算符的情况并非如此


因此
(++(++i))在第二个操作获得左值时编译,但是
(++(i++)
没有。可以将
左值
分配给<代码>右值
是可以赋值的。不确定这是否太宽而不能重复,但您可能可以在这里找到答案:括号可以让代码显示出来,所以即使它编译了,运行时行为也不确定,程序的格式也不正确。@juanchopanza Ok。现在,当我们说“在C中,增量前和增量后都返回右值”时,@ARBY
5
是右值。左值在哪里?
左值
是可以分配给的东西<代码>右值
是可以赋值的。不确定这是否太宽而不能重复,但您可能可以在这里找到答案:括号可以让代码显示出来,所以即使它编译了,运行时行为也不确定,程序的格式也不正确。@juanchopanza Ok。现在,当我们说“在C中,增量前和增量后都返回右值”时,@ARBY
5
是右值。左值在哪里?
"lvalue" and "rvalue" are so named because of where each 
  of them can appear in an assignment operation.  An 
  lvalue 
 can appear on the left side of an assignment operator, 
  whereas an rvalue can appear on the right side.

  As an example:

int a;
a = 3;

In the second line, "a" is the lvalue, and "3" is the rvalue.

 in this example:



int a, b;
a = 4;
b = a;

In the third line of that example, "b" is the lvalue, and "a" is 
the rvalue, whereas it was the lvalue in line 2.  This 
illustrates an important point: An lvalue can also be an 
rvalue, but an rvalue can never be an lvalue.

Another definition of lvalue is "a place where a value can 
be stored."