-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlvalue_rvalue_references.cpp
88 lines (65 loc) · 1.62 KB
/
lvalue_rvalue_references.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
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
using namespace std;
void swap(int &, int &);
double &valueAtIndex(double arr[], int i);
double valueAtIndex2(double arr[], int i);
int &getValue(int &);
void printArr(double arr[], int size);
int main()
{
int a = 10;
int b = 20;
swap(a, b);
cout << "(a,b):" << a << "," << b << endl;
double arr[] = {11.12, 23.43, 621.54, 54.893, 23.52};
printArr(arr, 5);
double *d = &valueAtIndex(arr, 0); // equivalue to - *p
cout << "address D: " << d << endl;
cout << "value *D: " << *d << endl;
// Since reference is nothing but an
// alias(synonym) of another variable,
// the address never changes.
int p = 1900;
int &q = getValue(p);
cout << "p: " << p << endl;
cout << "addr of p:" << &p << endl;
cout << "q: " << q << endl;
cout << "addr of q:" << &q << endl;
// since the function just returns a ref to
// p (ref is just another name) therefore,
// we can even set the new value.
getValue(p) = 2500;
cout << "After: " << endl;
cout << "p: " << p << endl;
cout << "addr of p:" << &p << endl;
cout << "q: " << q << endl;
cout << "addr of q:" << &q << endl;
return 1;
}
// returns a reference of variable
int &getValue(int &x)
{
return x;
}
double &valueAtIndex(double arr[], int i)
{
return arr[i];
}
double valueAtIndex2(double arr[], int i)
{
return arr[i];
}
void printArr(double arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << "arr[" << i << "]: " << arr[i] << " ";
}
cout << endl;
}
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}