Broad Network


C++ Lvalue and Rvalue

Some Features of C++ Entities – Part 12

Forward: In this part of the series, I talk about what is known as lvalues and rvalues in C++.

By: Chrysanthus Date Published: 25 Sep 2012

Introduction

This is part 12 of my series, Some Features of C++ Entities. In this part of the series, I talk about what is known as lvalues and rvalues in C++. You should be reading the parts of this series in the order given. Lvalue is pronounced, "el-value". Rvalue is pronounced, "are-value".

Simple Definitions
In simple terms, an lvalue is an identifier whose value can both be read and changed; an rvalue is an identifier whose value can only be read but not changed. So, in the statement,

    int ident = 54;

ident is an lvalue since its value can be read and changed by assignement down in the code. In the statement,

    const int ident = 54;

ident is an rvalue since its value can only be read and not changed down in the code. I give you the official definitions of lvalue and rvalue below in this tutorial.

Related Lvalue Expressions
Some expressions boil down ito identifiers. Consider the following expression:

arr[2]

This expression identifiers a value in an array. Through the expression, the value can be read and changed. So arr[2] is an lvalue expression.

Consider the following statement:

    int *pointer = &pointed;

In this statement, the expression, "*pointer" indicates a pointer whose value (address) can be read and changed down in the code, by assigning a new address (new &pointed). So the expression is an lvalue expression.

Consider the following statement:

    *pointer = 26;

In the statement, the expression, "*pointer" here, identifies a pointed object whose value can be read and changed down in the code, by assigning a new value. So the expression is an lvalue expression.

Consider the following statement:

    int &herInt = hisInt;

In the statement, the expression, "&herInt" is an address whose object value can be read and changed down in the code, by assigning a new value to herInt. So the expression is an lvalue expression.

Related Rvalue Expressions
As mentioned above, some expressions boil down to identifiers. Consider the following declaration expression:


    const int *myPointr

Here, the value of the pointed object is constant, meaning it can only be read but not changed. So the whole expression is an rvalue expression.

Consider the following declaration expression:

    int *const myPointr

Here, the pointer is made constant, meaning its address (value) can only be read but not changed. So the whole expression is an rvalue expression.

Definition of Lvalue and Rvalue
As you can see from the above, an expression can boil down to an identifier. An lvalue is defined as an expression whose value can be read and changed. An rvalue is defined as an expression whose value can only be read but not changed. An identifier is the simplest expression.

Prototype with const at End
A class member function prototype that has the specifier "const" at the end, can only use (read) the data members (properties) of its class in its function body, but cannot change their values in the function body. Such a function is said to work in an rvalue fashion.

This is the end of the article. And we are at the end of the series; I hope you appreciated it.

I assume you have completed this series. It means you already had the professional skills in C++. Now, you are in an advanced course of C++. You may already be working. If you ever have too much work, you can subcontract (outsource) some or all to someone in the site below.

Chrys

Programming Solutions

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem

Comments

Become the Writer's Fan
Send the Writer a Message