涓嬮潰榪欐寮曟枃鍙宸﹀箋佸彸鍊煎姞娣辯悊瑙c?br />
鍙傝冩枃鐚細銆夾NSI/ISO C++ Professional Programmer's Handbook銆?/p>
lvalues and rvalues
An object is a contiguous region of storage. An lvalue is an expression that refers to such an object. The original
definition of lvalue referred to an object that can appear on the left-hand side of an assignment. However, const
objects are lvalues that cannot be used in the left-hand side of an assignment. Similarly, an expression that can appear
in the right-hand side of an expression (but not in the left-hand side of an expression) is an rvalue. For example
#include聽
<
string
>
using
聽
namespace
聽std;
int
&
聽f();
void
聽func()
{
聽聽聽聽
int
聽n;
聽聽聽聽
char
聽buf[
3
];
聽聽聽聽n聽
=
聽
5
;聽
//
聽n聽is聽an聽lvalue;聽5聽is聽an聽rvalue
聽聽聽聽buf[
0
]聽
=
聽
'
a
'
;聽
//
聽buf[0]聽is聽an聽lvalue,聽'a'聽is聽an聽rvalue
聽聽聽聽
string
聽s1聽
=
聽
"
a
"
,聽s2聽
=
聽
"
b
"
,聽s3聽
=
聽聽
"
c
"
;聽
//
聽"a",聽"b",聽"c"聽are聽rvalues
聽聽聽聽s1聽
=
聽
//
聽lvalue
聽聽聽聽s2聽
+
s3;聽
//
s2聽and聽s3聽are聽lvalues聽that聽are聽聽implicitly聽converted聽to聽rvalues
聽聽聽聽s1聽
=
聽
//
lvalue
聽聽聽聽
string
(
"
z
"
);聽
//
temporaries聽are聽rvalues
聽聽聽聽
int
聽
*
聽p聽
=
聽
new
聽
int
;聽
//
p聽is聽an聽lvalue;聽'new聽int'聽is聽an聽rvalue
聽聽聽聽f()聽
=
聽
0
;聽
//
a聽function聽call聽that聽returns聽a聽reference聽is聽an聽lvalue
聽聽聽聽聽s1.size();聽
//
otherwise,聽a聽function聽call聽is聽an聽rvalue聽expression
}
An lvalue can appear in a context that requires an rvalue; in this case, the lvalue is implicitly converted to an rvalue.
An rvalue cannot be converted to an lvalue. Therefore, it is possible to use every lvalue expression in the example as
an rvalue, but not vice versa.

]]>