lvalue vs rvalue in c++11

What is lvalue ?
lvalue is anything whose address is accessible. It means we can take address of lvalue using & operator.
int x = 1;
In above expression,  address of x can be accessed using expression below, therefore x is lvalue.
int * ptr = &x;
Now consider this example 
int y = x + 1;
In above expression y  is lvalue but  x + 1 is not lavlue. We cant access address of x + 1
int * ptr3 = &(x+1); // Compile Error

What is rvalue ? 
rvalue is anything that is not lvalue. We cannot take address of rvalue and it also copy of rvalue don’t persist beyond the single expression.
Lets see few example of rvalue

int x = 10;
int * ptr = &(10); // Compile Error
We can not access the address of 10
its copy destroys when program control goes beyond the expression after ;
Consider below example, 
int readData()
{
int data = 0;
return data;
}
int * ptr = &readData();//Compile error  // - Cannot access address of rvalue

readData() is a rvalue and we cannot take the address of rvalue.

Thats's all for now!

No comments:

Post a Comment

Rendering 3D maps on the web using opesource javascript library Cesium

This is a simple 3D viewer using the Cesium javascript library. The example code can be found here . Click on question-mark symbol on upp...