C ++ left and right, left and right
What is lvalue and rvalue?
Lvalue:Objects with storage properties, that is, lvalue objects, refer to object objects that actually occupy memory space and have memory addresses, such as variables, functions, and function pointers.
Rvalue:Compared with lvalue, lvalue is a non-stored object, that is, a temporary object.
It can also be understood as follows:
Lvalue:It can be used to locate the location value in the memory, which is located on the left of the assignment operator and can be assigned a value.
Rvalue:Register value is located on the left of the value assignment operator and cannot be assigned a value.
The most common misunderstanding of the left and right values is that the left side of the equal sign is the left side, and the right side of the equal sign is the right side. Both the left and right values are for expressions. The left value is a persistent object that exists after the expression ends, and the right value is a temporary object that no longer exists at the end of the expression.
For example:
int a = 10; int b = 20; int *pFlag = &a; vector<int> vctTemp; vctTemp.push_back(1); string str1 = "hello "; string str2 = "world"; const int &m = 1;
Excuse me, a, B, a + B, a ++, ++ a, pFlag, * pFlag, vctTemp [0], 100, string ("hello"), str1, is str1 + str2 and m the Left or Right values? ,
- Both a and B are persistent objects (addresses can be obtained) and are left values;
- A + B is a temporary object (the address cannot be obtained) and is the right value;
- A ++ extracts a copy of Persistent Object a first, adds 1 to the value of Persistent Object a, and returns the copy, the copy object is a temporary object (the address cannot be obtained), so it is the right value;
- ++ A adds 1 to the value of Persistent Object a and returns the permanent object a itself (which can be an address). Therefore, it is the left value;
- Both pFlag and * pFlag are persistent objects (addresses can be obtained), which are left values;
- VctTemp [0] calls the overloaded [] operator, while the [] OPERATOR returns an int &, which is a persistent object (the address can be taken for it) and is the left value;
- 100 and string ("hello") are temporary objects (addresses cannot be obtained), which are the right values;
- Str1 is a persistent object (the address can be obtained), which is the left value;
- Str1 + str2 calls the + operator, while the + operator returns a string (the address cannot be obtained), so it is the right value;
- M is a constant reference that refers to a right value, but the reference itself is a persistent object (which can be an address), which is the left value.
For example:
int a = 5;
The bucket of variable a is & a (lvalue OF a), and the value of Bucket & a is 5 (rvalue OF ).
Let's look at another example:
int* p = NULL; p = new int(5);
For p: lvalue is & p (p address), rvalue is the value on & p address.
For * p: lvalue is p (* p address), rvalue is the value on p address.
With this, you can better understand the left value reference.