C++ Programming

< C++ Programming‎ | Programming Languages‎ | C++‎ | Code‎ | Keywords‎ | static
Internal linkage

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:

static int apples = 15;
defines a "static global" variable named apples, with initial value 15, only visible from this translation unit.
static int 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.
static const int muffins_per_pan=12;
defines is a variable named muffins_per_pan, visible only in this translation unit. The static keyword is redundant here.
const int hours_per_day=24;
defines a variable named hours_per_day, only visible in this translation unit. (This acts the same as
static const int hours_per_day=24;
static void 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.
static void 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.