-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreferences.cpp
58 lines (32 loc) · 884 Bytes
/
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
#include<iostream>
int test(int& a, int& b) {
std::cout << a << " " << b << std::endl;
return (a + b);
}
void swap(int& a, int& b) { // important to put the reference beacause it will swap the "real" value of c and d
int temp = a;
a = b;
b = temp;
}
int main() {
int a = 10;
int b = 6;
test(a, b);
int c = 10;
int d = 20;
swap(c, d);
std::cout << " new value of c is: " << c << std::endl;
std::cout << "new value of d is: " << d << std::endl;
int i; // an int
int& j = i; // j alias of i, ("j is also under the name of i")
j = 5;
std::cout << j << " " << i << std::endl;
i = 2;
std::cout << j << " " << i << std::endl;
int t; // an int
int* p; // a pointer
p = &t; // the pointer hold the adress of t (&t). p points to t;
*p = 4; // deference and give it a value;
std::cout << "value of t is: "<< t << std::endl;
system("pause");
}