2

I've started learning C++ via this page.

The concepts of rvalue and lvalue were confusing for me, especially this:

Note: const variables are considered non-modifiable l-values.

I don't understand that const variable can be lvalue. Is there any example code that the const variable is used as lvalue in practice? I see no difference between rvalue and non-modifiable lvalue.

MyBug18
  • 2,135
  • 2
  • 11
  • 25
  • What exactly do you believe about a `const` object that prevents it from being an lvalue? What does "lvalue" mean to you? – Sam Varshavchik Mar 31 '20 at 03:31
  • I thought that the term "lvalue" means which can be at the leftside of assignment statement. – MyBug18 Mar 31 '20 at 03:33
  • 1
    @JerryJeremiah Both `a[i]` and `*(p+1)` are lvalues (unless `a` is an rvalue, in which case `a[i]` is an xvalue). – L. F. Mar 31 '20 at 03:59
  • @JerryJeremiah your comment is completely wrong . lvalues and rvalues are expressions , either might designate a named or unnamed object, and being expressions they "exist" as compilation constructs only. You're mixing up objects with expressions . – M.M Mar 31 '20 at 04:28
  • `const` tells you if a variable can be modified or not. rvalue/lvalue tells you the value category. Both rvalues and lvalues can be modified. I don't really understand why an rvalue and a non-modifiable lvalue would be the same. You are comparing two different things that are not really related. – super Mar 31 '20 at 05:08

1 Answers1

0

Let assume you have statement like this

const int con=10;     //con is non-modifiable l-value
  • you can not modify value of con .
  • But con has address(&con).

So it is non-modifiable l-value.

code:

#include<iostream>

int main(void)
{
    const int con=10; //con is non-modifiable l-value
    std::cout<<con;
}
srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25