The left and right values in C ++

Source: Internet
Author: User
1. Concept
Both variables and text constants have storage areas and related types. The difference is that variables are addressable;

Each variable has two values associated with it:
1> the data value is stored in a memory address, also known as the right value (Rvalue), and the right value is the read value ), both literal constants and variables can be used for the right value.
2> the address value, that is, the memory address that stores the data value, also known as the left value (lvalue). The text constant cannot be used as the left value.

2. Problem
Add brackets to the expression: ++ --
Result ++ (--)

This expression is invalid because the previous incremental operation requires a modifiable left value, while "A --" is not the left value (that is, the right value)

3. Differences between pre-increment and post-Increment
EarlyC LanguageThe for loop statement is usually written as follows:
For (INT I = 0; I <10; I ++)
Most of them are:
For (INT I = 0; I <10; ++ I)

Are there any differences between the two?

A ++ returns the value of A, and then adds 1 to the variable A. The returned result must generate a temporary variable similar
{
Int temp =;
A = a + 1;
Return temp; // return the right value
}

++ A is:
{
A = a + 1;
Return & A; // return the left value.
}

Obviously, the former increment does not require intermediate variables and is more efficient.

Another article: Meanings and misunderstandings of the left and right values of C ++

The terms "L-Values" and "R-values" are easily confused because their historical origins are also confused. They first originated from the compiler designer, literally the value on the left of the expression and the value on the right of the expression. Their meanings have been evolving, but their names have not changed. Now they are "real. Although left-value and right-value are still called, their meanings are quite different.

C ++ 03 is written as follows: "Each expression is either an lvalue or an rvalue ."

Remember, lvalue and rvalue are for expressions.

Lvalue refers to the persistent objects that exist after the end of a single expression. For example, OBJ, * PTR, PRT [Index], ++ X are all lvalues.

Rvalue refers to the temporary objects that no longer exist at the end of the expression (at the semicolon. For example: 1729, X + Y, STD: string ("meow"), and X ++ are both rvalues.

The difference between ++ X and X ++ is semantic: When int I = 10; is written, I is an lvalue, which actually represents an address in the memory, is persistent. Expression ++ X is also an lvalue, which modifies the value of X, but still represents the original Persistent Object. However, the expression I ++ is an rvalue. It just copies an initial value of I, modifies the value of I, and returns the temporary copy, which is a temporary object. Both ++ I and I ++ increase the I, but ++ I returns the I itself, while I ++ returns the temporary copy. This is why ++ I is an lvalue while I ++ is an rvalue.

The difference between lvalue and rvalue lies not in what the expression does, but in what the expression represents (A Persistent object or a temporary product ). To determine whether an expression is lvalue, the direct method is "Can I retrieve the address of the expression ?", If yes, It is an lvalue; if not, it is an rvalue.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.