-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconst.cpp
37 lines (24 loc) · 806 Bytes
/
const.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
#include <iostream>
// const
// - A compile time constraint that an object can not be modified
// - Means its immutable
// Advantages:
// - const means the variable can be put in ROM (usefull in Embedded programming)
// - Guards againts inadvertent write to the variable
// - Self documenting
int main() {
const int i = 10;
// If const is on left of *, data is const
// If const is on right of *, pointer is const
const int *p1 = &i; // data is const but pointer is not
// int* const p2; // pointer is const but data is not
const int* const p3 = &i; // both pointer and data is const
// so these are the same
int const *p4;
const int *p5;
// Casting away the const
const_cast<int&>(i) = 42;
// Making static to const
int j;
static_cast<int&>(j);
}