In these languages, the term "static variable" has two meanings which are easy to confuse:
- A variable with the same lifetime as the program, as described above (language-independent); or
- (C-family-specific) A variable declared with storage class
static
.
As well as specifying static lifetime, declaring a variable as
static
can have other effects depending on where the declaration occurs:- Static global variable: a variable declared as
static
at the top level of a source file (outside any function definitions) is only visible throughout that file ("file scope", also known as "internal linkage"). - Static local variables: variables declared as
static
inside a function are statically allocated, thus keep their memory cell throughout all program execution, while having the same scope of visibility as automatic local variables, meaning remain local to the function. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again. - static member variables: in C++, member variables declared as
static
inside class definitions are class variables (shared between all class instances, as opposed to instance variables).
No comments:
Post a Comment