-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl_r_values.cpp
75 lines (66 loc) · 1.77 KB
/
l_r_values.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*rvalues indicate objects eligible for move operations, while lvalues generally don't.
In concept (though not always in practice), rvalue correspond to temporary objects
returned from functions, while lvalues correspond to objects you can refer to, either
by name or by following a pointer or lvalue reference*/
#include <iostream>
using namespace std;
int square(int m)
{
int sqr = m*m;
return sqr;
}
int square2(int& x) { return x*x; }
int square3(const int& x) { return x*x; }
struct examp
{
int * ptr;
examp(){
ptr = new int;
}
examp(const examp & a1){ //copy constructor
this->ptr = new int;
}
~examp(){
delete ptr;
}
};
int main()
{
{
// lvalue examples
int i = 7; // i: lvalue
int *pi = &i; // i is addressable
i = 10; // we can modify it
class cat {};
cat c; // c is an lvalue for a user defined type
}
// rvalue examples
int i = 7; // i: lvalue but 7 is rvalue
int k = i+3; // (i+3) is an rvalue
//int *pi = &(i + 3); // error, it's not addressable
//i + 3 = 10; // error - cannot assign a value to it
//3 = i; // error - not assignable
class cat {};
//c = cat(); // cat() is an rvalue
int sq = square(10); // square(10) is an rvalue
cout<< sq <<endl;
{
//rvalue and lvalue- Reference
int i;
int &r = i;
//int &r; = 7; // error: lvalue cannot be bound to rvalue 7
//However, we can bind an rvalue to a const lvalue reference (const reference):
const int&v = 7;
cout << v << endl;
square2(i); // OK
//square(7); // error, 7 is an rvalue and cannot be assigned to a reference
///Is there a way to pass 7 to the square function? Seems I've seen it working.
//Yes, we can. The const is here for us!
square3(i); // OK
square3(7); // OK
}
{
int &&k = 19; //rvalue reference
cout << ++k <<endl;
}
}