April 2017
Beginner to intermediate
394 pages
9h 16m
English
The second way is by having a variable or function in a class being defined as static. Normally, when you create an instance of a class, the compiler has to set aside additional memory for each variable that is contained inside of the class in consecutive blocks of memory. When we declare something as static, instead of creating a new variable to hold data, a single variable is shared by all of the instances of the class. In addition, since it's shared by all of the copies, you don't need to have an instance of the class to call it. Take a look at the following bolded code to create our variable:
class StaticExamples { public: static float classVariable; static void StaticFunction() { // Note, can ...Read now
Unlock full access