The static keyword
Another thing that is important to know before diving into the Singleton pattern is what the static
keyword means, as it's something that we will be using the functionality of when building this pattern. When we use the static
keyword, there are three main contexts that it'll be used in:
- Inside a function
- Inside a class definition
- In front of a global variable in a program with multiple files
Static keyword inside a function
The first one, being used inside of a function, basically means that once the variable has been initialized, it will stay in the computer's memory until the end of the program, keeping the value that it has through multiple runs of the function. A simple example would be something like this:
#include <string> class StaticExamples { public: void InFunction() { static int enemyCount = 0; // Increase the value of enemyCount enemyCount += 10; std::string toDisplay = "\n Value of enemyCount: " + std::to_string...