<< Chapter < Page Chapter >> Page >
Concepts and examples of Lvalue and Rvalue within the C++ programming language.

Discussion

They refer to on the left and right side of the assignment operator. The Lvalue (pronounced: L value) concept refers to the requirement that the operand on the left side of the assignment operator is modifiable, usually a variable. Rvalue concept pulls or fetches the value of the expression or operand on the right side of the assignment operator. Some examples:

int age; // variable set up then later in the programage = 39;

Got questions? Get instant answers now!

The value 39 is pulled or fetched (Rvalue) and stored into the variable named age (Lvalue); destroying the value previously stored in that variable.

int age; // variable set up int voting_age = 18; // variable set up with initializationthen later in the program age = voting_age;

Got questions? Get instant answers now!

If the expression has a variable or named constant on the right side of the assignment operator, it would pull or fetch the value stored in the variable or constant. The value 18 is pulled or fetched from the variable named voting_age and stored into the variable named age.

If the expression is a test expression or Boolean expression , the concept is still an Rvalue one. The value in the identifier named age is pulled or fetched and used in the relational comparison of less than.

const int JACK_BENNYS_AGE = 39; // constant set up then later in the programJACK_BENNYS_AGE = 65;

Got questions? Get instant answers now!

This is illegal because the identifier JACK_BENNYS_AGE does not have Lvalue properties. It is not a modifiable data object, because it is a constant.

Some uses of the Lvalue and Rvalue can be confusing.

int oldest = 55; // variable set up with initialization then later in the programage = oldest++;

Got questions? Get instant answers now!

Postfix increment says to use my existing value then when you are done with the other operators; increment me. Thus, the first use of the oldest variable is an Rvalue context where the existing value of 55 is pulled or fetched and then assigned to the variable age; an Lvalue context. The second use of the oldest variable is an Lvalue context where in the value of oldest is incremented from 55 to 56.

Definitions

Lvalue
The requirement that the operand on the left side of the assignment operator is modifiable, usually a variable.
Rvalue
Pulls or fetches the value stored in a variable or constant.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?

Ask