What are static variables designed for? What\'s the difference between static int and int?
Static variables are initialized in the data segment (on x86; modify as appropriate for other architectures) instead of on the stack. They persist for the life of the program instead of vaporizing once they go out of scope.
The static
keyword has four separate uses, only two of which are closely related:
Both static data members and static local variables can become hidden global state, and should be used carefully.
Now which two are closely related? It's not the two class members—the warning about global state gives it away. You can consider static data members as static local variables, where the functions to which they belong are all methods of the class, instead of a single function.
I found many related questions, but, surprisingly, no duplicates.
A static member can be referenced without an instance.
See the "Static Members" section here: http://www.cplusplus.com/doc/tutorial/classes2/