static
It's all about scope, duration and linkage!
outside of a block
- It defines a global variable with internal linkage, meaning the variable could only be used in the file in which it was defined.
inside a block
- Using the static keyword on local variables changes them from automatic duration to static duration.
- A static variable retains its value even after the scope in which it has been created has been exited!
- Static duration variables are only created (and initialized) once, and then they are persisted throughout the life of the program.
inside a class
variable
- shared by all objects of the class
- exist even if no objects of the class have been instantiated
- belong to the class itself, not to the objects of the class
function
- not attached to any particular object
- can be called directly by using the class name and the scope resolution operator
- have no this pointer! (the this pointer always points to the object that the member function is working on)
- can only access static member variables.