-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathconst.cpp
94 lines (68 loc) · 1.39 KB
/
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
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
89
90
91
92
93
94
// Copyright (c) 2020 by Chrono
//
// g++ const.cpp -std=c++11 -o a.out;./a.out
// g++ const.cpp -std=c++14 -o a.out;./a.out
// g++ const.cpp -std=c++14 -I../common -o a.out;./a.out
#include <iostream>
void case1()
{
using namespace std;
//const int MAX_LEN = 1024;
const volatile int MAX_LEN = 1024;
const std::string NAME = "metroid";
auto ptr = (int*)(&MAX_LEN);
*ptr = 2048;
cout << MAX_LEN << endl;
}
void case2()
{
using namespace std;
int x = 100;
const int& rx = x;
const int* px = &x;
cout << rx << *px << endl;
string name = "uncharted";
const string* ps1 = &name;
//*ps1 = "spiderman";
cout << *ps1 << endl;
string* const ps2 = &name;
*ps2 = "spiderman";
cout << *ps2 << endl;
const string* const ps3 = &name;
}
class DemoClass final
{
private:
using mutex_type = int; // dummy type
private:
mutable mutex_type m_mutex;
const long MAX_SIZE = 256;
int m_value;
public:
int get_value() const
{
return m_value;
}
void save_data() const
{
m_mutex++;
}
};
constexpr
int fib(int n)
{
if (n == 0 || n == 1) {
return 1;
}
return fib(n - 1) + fib(n - 2);
}
int main()
{
using namespace std;
case1();
case2();
constexpr
int fib5 = fib(5);
cout << fib5 << endl;
cout << "const demo" << endl;
}