-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlvalue_rvalue_references2.cpp
More file actions
50 lines (33 loc) · 897 Bytes
/
lvalue_rvalue_references2.cpp
File metadata and controls
50 lines (33 loc) · 897 Bytes
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
#include <iostream>
using namespace std;
int &getValue(int &);
int main()
{
int a = 1500;
// the function returns a ref to a
// ref is just another name for the
// same variable, in this case 'a'.
int &ra = getValue(a);
cout << "a: " << a << endl;
cout << "ra: " << ra << endl;
cout << "&a: " << &a << endl;
cout << "&ra: " << &ra << endl;
cout << "Changing values via ra: " << endl;
ra = 15;
cout << "a: " << a << endl;
cout << "ra: " << ra << endl;
cout << "&a: " << &a << endl;
cout << "&ra: " << &ra << endl;
cout << "Changing values via function: " << endl;
getValue(ra) = 5600;
cout << "a: " << a << endl;
cout << "ra: " << ra << endl;
cout << "&a: " << &a << endl;
cout << "&ra: " << &ra << endl;
return 1;
}
// a function that returns reference
int &getValue(int &a)
{
return a;
}