The static keyword in C++ when used in different contexts leads to different features (sometimes confusing).
Static Variables
A static variable declared at file level or at namespace scope, is a global variable with a lifetime for the entire execution of the program and is only accessible by code within the translation unit (the file or namespace). The C++ Specification refers to this as Internal Linkage. Any other file which includes another file declaring a static variable gets its own copy. It is possible for multiple translation units to contain their own copy of the same static variable.
Here's an example of a static variable file_level_global being included in both main.cpp and static1.cpp through the include directive -
A static variable in a function, like a local variable i.e. can't be accessed from outside the function, however its initialized only once when the the first time the execution of the program reaches the line where they are declared.
Static variables in a class are accessible outside the class through the class scope or within the non-static member functions of the class.
The C++ Specification uses the term 'Static Storage Duration' when describing both, static variables in a function and class i.e. the data will reside in the same place in memory for the life of the program
Static Functions
Static functions within a file are not accessible outside the file. The static keyword is used to signify that this function is to be used within the translation unit. If a static function is declared in a header, every file that includes the header will get its own copy of the function.
Static member functions of a class can be called using class scope and they can access static data members. Non-static data cannot be accessed by static member functions. However non-static member functions can access the static data members of the class.
Static Variables
A static variable declared at file level or at namespace scope, is a global variable with a lifetime for the entire execution of the program and is only accessible by code within the translation unit (the file or namespace). The C++ Specification refers to this as Internal Linkage. Any other file which includes another file declaring a static variable gets its own copy. It is possible for multiple translation units to contain their own copy of the same static variable.
Here's an example of a static variable file_level_global being included in both main.cpp and static1.cpp through the include directive -
A static variable in a function, like a local variable i.e. can't be accessed from outside the function, however its initialized only once when the the first time the execution of the program reaches the line where they are declared.
Static variables in a class are accessible outside the class through the class scope or within the non-static member functions of the class.
The C++ Specification uses the term 'Static Storage Duration' when describing both, static variables in a function and class i.e. the data will reside in the same place in memory for the life of the program
Static Functions
Static functions within a file are not accessible outside the file. The static keyword is used to signify that this function is to be used within the translation unit. If a static function is declared in a header, every file that includes the header will get its own copy of the function.
Static member functions of a class can be called using class scope and they can access static data members. Non-static data cannot be accessed by static member functions. However non-static member functions can access the static data members of the class.