-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.cpp
69 lines (59 loc) · 2.23 KB
/
static.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
// https://www.youtube.com/watch?v=f3FVU-iwNuA&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=21&ab_channel=TheCherno
// https://www.youtube.com/watch?v=f7mtWD9GdJ4&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=48&ab_channel=TheCherno
#include <iostream>
static void Function1()
{
// if there was a function with the same name in another file
// we would get a linker error
}
void Function2()
{
static int i = 0; // a static variable is only initialized once
// and it is not destroyed when it goes out of scope
// it is only accessible in this function
// it is initialized when the program starts
// and destroyed when the program ends
// it is useful for caching values or counting how many
// times a function has been called
i++;
std::cout << i << std::endl;
}
class Singleton
{
public:
int value = 0;
public:
static Singleton &Get()
{
static Singleton instance;
return instance;
}
};
static int s_Variable = 5; // only accessible in this file
// the linker won't look for it outside this file
extern int variable; // tells the linker to look for this variable outside this file
// this is the default behavior
// this is the same as not writing "static"
// this is useful for global variables
struct Struct // The static keyword can also be used with structs and classes
{
inline static int s_Member; // only one instance of this variable
// shared by all instances of this class
// can be accessed with Struct::s_Member
static void Method()
{
// doesn't use the class instance
}
};
int main()
{
Function2(); // prints 1
Function2(); // prints 2
Function2(); // prints 3
// another great example of using static in a function
Singleton::Get().value = 4;
std::cout << Singleton::Get().value << std::endl;
// note: "Struct::s_Member" would have to be defined before this if it wasn't declared inline (C++17)
Struct::s_Member = 2;
Struct::Method();
}