-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
37 lines (32 loc) · 1.01 KB
/
Copy pathmain.cpp
File metadata and controls
37 lines (32 loc) · 1.01 KB
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
#include <iostream>
int main() {
// int a = 1;
// int b = a;
// std::cout << "before a: " << a << std::endl;
// std::cout << "before b: " << b << std::endl;
// b = 5;
// std::cout << "after a: " << a << std::endl;
// std::cout << "after b: " << b << std::endl;
// std::cout << "addr a: " << &a << std::endl;
// std::cout << "addr b: " << &b << std::endl;
// `b` is an alias to `a`
int a = 1;
int& b = a;
b = 5;
// std::cout << "after a: " << a << std::endl;
// std::cout << "after b: " << b << std::endl;
// std::cout << "addr a: " << &a << std::endl;
// std::cout << "addr b: " << &b << std::endl;
// Compiler error: Must be init immediately.
// Reference is an alias, so it must bind to valid object.
// C++ language does not have concept of empty reference.
// int a = 1;
// int& b; // cannot be null
// b = a;
// `c` will always be an alias to `b`, value inside of it can change
// int a = 1;
// int b = 2;
// int& c = b;
// c = a; // c still refers to b
return 0;
}