To do:
Alter the above links from subsection to book locations after the structure is fixed.

The static keyword can also be used on functions, inside functions, on classes, on classes members (data and functions), in structs, unions (but not in a union's member) we will cover each use separately.

Using the static modifier makes a variable have static lifetime and on global variables makes them require internal linkage (variables will not be accessible from code of the same project that resides in other files).

static lifetime

Means that a static variable will need to be initialized in the file scope and at run time, will exist and maintain changes across until the program's process is closed, the particular order of destruction of static variables is undefined.

static variables instances share the same memory location. This means that they keep their value between function calls. For example, in the following code, a static variable inside a function is used to keep track of how many times that function has been called:

When used on a free function, a global variable, or a global constant, it specifies internal linkage (as opposed to extern, which specifies external linkage). Internal linkage limits access to the data or function to the current file.

Examples of use outside of any function or class:

staticint apples =15;

defines a "static global" variable named apples, with initial value 15, only visible from this translation unit.

staticint bananas;

defines a "static global" variable named bananas, with initial value 0, only visible from this translation unit.

int g_fruit;

defines a global variable named g_fruit, with initial value 0, visible from every translation unit. Such variables are often frowned on as poor style.

staticconstint muffins_per_pan=12;

defines is a variable named muffins_per_pan, visible only in this translation unit. The static keyword is redundant here.

constint hours_per_day=24;

defines a variable named hours_per_day, only visible in this translation unit. (This acts the same as

staticconstint hours_per_day=24;

).

staticvoid f();

declares that there is a function f taking no arguments and with no return value defined in this translation unit. Such a forward declaration is often used when defining mutually recursive functions.

staticvoid f(){;}

defines the function f() declared above. This function can only be called from other functions and members in this translation unit; it is invisible to other translation units.

Member functions or variables declared static are shared between all instances of an object type. Meaning that only one copy of the member function or variable does exists for any object type.

member functions callable without an object

When used in a class function member, the function does not take an instantiation as an implicit this parameter, instead behaving like a free function. This means that static class functions can be called without creating instances of the class:

Named constructors are a good example of using static member functions. Named constructors is the name given to functions used to create an object of a class without (directly) using its constructors. This might be used for the following:

To circumvent the restriction that constructors can be overloaded only if their signatures differ.

Making the class non-inheritable by making the constructors private.

Preventing stack allocation by making constructors private

Declare a static member function that uses a private constructor to create the object and return it. (It could also return a pointer or a reference but this complication seems useless, and turns this into the factory pattern rather than a conventional named constructor.)

Here's an example for a class that stores a temperature that can be specified in any of the different temperature scales.

The use of the static specifier in a data member, will cause that member to be shared by all instances of the owner class and derived classes. To use static data members you must declare the data member as static and initialize it outside of the class declaration, at file scope.

When used in a class data member, all instantiations of that class share one copy of the variable.

In the example above, the static class variable numFoos is shared between all three instances of the Foo class (f1, f2 and f3) and keeps a count of the number of times that the Foo class has been instantiated.