Skip to content

Commit d5cc12d

Browse files
committed
-> added more on l-value references
1 parent 16d6909 commit d5cc12d

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

notes_7_l_val_r_val.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int add10(int x)
6+
{
7+
return x + 10;
8+
}
9+
10+
int main()
11+
{
12+
// What is L-val and R-val?
13+
// l value are variables or entities in C++ that
14+
// can have memory associated with it.
15+
// Whereas r-value are entities that do not have
16+
// memory associated with it.
17+
// For eg.
18+
19+
int a = 5;
20+
21+
// here a is a l-value. There is some memory included
22+
// for the variable a. Whereas, '5', there is no memory.
23+
// Its just a constant. This object lasts longer.
24+
// Or have a longer lifespan.
25+
26+
// Furthermore, we can get that memory using &
27+
28+
cout << "Memory: " << &a << endl;
29+
cout << "Value: " << a << endl;
30+
31+
// Such variables are called l-values.
32+
33+
// R-values, these are entites and objects, that
34+
// doesn't have memory associated with it.
35+
// There lifespan is shorter too.
36+
37+
// For eg. '5' above is temporary.
38+
// These are called r-values.
39+
40+
// One more example
41+
42+
int x = 60;
43+
int y = 40;
44+
int sum = x + y;
45+
46+
// In this case the 'sum' is an l-value, and the expression,
47+
// "x + y" is a r-value.
48+
// We can get the memory of sum using &sum. But we cannot
49+
// get the memory of the expression "&(x + y)".
50+
51+
cout << "&sum: " << &sum << endl;
52+
// cout << "&(x + y): " << &(x + y) << endl;
53+
54+
// Furthermore, we cannot get the reference to a
55+
// function, for eg. the below is wrong.
56+
int eleven = add10(20);
57+
58+
return 1;
59+
}

notes_8_l_val_ref.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int getIncValue(int x)
6+
{
7+
return x * 100;
8+
}
9+
10+
int &getIncValue2(int &x)
11+
{
12+
return x;
13+
}
14+
15+
int main()
16+
{
17+
int a = 10;
18+
int &ra = a;
19+
20+
// ra is just a reference to a, meaning its just an
21+
// another name for 'a'. Therefore, it has the same
22+
// value and the same memory address as of a.
23+
24+
cout << "a: " << a << endl;
25+
cout << "ra: " << ra << endl;
26+
27+
cout << "&a: " << &a << endl;
28+
cout << "&ra: " << &ra << endl;
29+
30+
// These are also called 'l-value' references.
31+
// Since we can get the memory associated with these
32+
// variables.
33+
34+
a = getIncValue(a);
35+
36+
cout << "a: " << a << endl;
37+
cout << "ra: " << ra << endl;
38+
39+
cout << "&a: " << &a << endl;
40+
cout << "&ra: " << &ra << endl;
41+
42+
// Now
43+
44+
// this is not allowed because '20' is an r-value
45+
// and we cannot get a reference to an r-value
46+
47+
// int &r = 20; // error
48+
49+
// however
50+
// if there is a const in front, then we are allowed
51+
// to get ref.
52+
53+
const int &r = 20;
54+
55+
cout << "&r: " << &r << endl;
56+
57+
// this happens because internally C++ creates a temp
58+
// variable and assign the ref of temp to r.
59+
// Like this,
60+
61+
int temp = 20;
62+
int &x = temp;
63+
64+
// l-val references returned by function
65+
// a value returned by a function, by default, is
66+
// treated as an r-value. Therefore we cannot get
67+
// a ref to that returned value.
68+
// For example the below is incorrect and throws error.
69+
70+
// int &h = getIncValue(10); // r-value, need l-value
71+
72+
// However, if we make our function return the reference
73+
// instead of the the regular value.
74+
// It will be accepted!
75+
// For example,
76+
77+
int &h = getIncValue2(a); // accepted! because the fnction returns a ref.
78+
79+
// There is one more important topic that
80+
// screwed my mind.
81+
82+
// Since the above function is just returning a referece
83+
// to a. That means, the returned value is just another name
84+
// for 'a'. And also the memory address is the same as 'a'.
85+
// Therefore wrting like below is valid.
86+
87+
cout << endl
88+
<< endl;
89+
90+
cout << "a: " << a << endl;
91+
cout << "&a: " << &a << endl;
92+
cout << "h: " << h << endl;
93+
cout << "&h: " << &h << endl;
94+
95+
// now,
96+
97+
getIncValue2(a) = 8888;
98+
99+
cout << "a: " << a << endl;
100+
cout << "&a: " << &a << endl;
101+
cout << "h: " << h << endl;
102+
cout << "&h: " << &h << endl;
103+
}

0 commit comments

Comments
 (0)