TITLE: the static keyword (Private email, 24 Apr 98) KANZE:kanze@gabi-soft.fr (J. Kanze) [snip] > First, since you are asking for the opposition "static" vs. "dynamic", I > will suppose that we are not concerned with the keyword "static", which > has five or six different meanings, since there is no keyword "dynamic". TRIBBLE: david.tribble@central.beasys.com The 'static' keyword has exactly four meanings. 1. Declaring a free (nonmember) function or variable as 'static' places it in the file-local namespace, i.e., its name has file scope. It has "internal" linkage, meaning the name is not visible outside the file. Since it is defined outside any function or block, its lifetime is the same as the lifetime of the program. (Note that you get the same effect by placing the name in an anonymous namespace in the file.) 2. Declaring a variable that is local to a function or a block as 'static' means that it has a lifetime that is not "auto", i.e., its value and memory allocation do not go away between invocations of the function or block. It's almost the same as (1), having "internal" linkage, but its name has block scope, meaning that it is not visible outside the block. Also, it has a slightly different lifetime than (1), in that it isn't constructed until the first invocation of the function or block; once constructed, though, it continues to exist as long as the program is active. 3. Declaring a member variable within a class as 'static' means that it is shared among all of the class's member functions (and friends), and that it has "program" (not auto) lifetime. Its value is not associated with any object of that class type, but rather it exists apart from any such objects. Its name has class scope (and thus is subject to access restrictions based on whether it is declared as 'public', 'protected', or 'private'). It has "external" linkage, meaning that its name is visible outside the file in which it is defined. (Thus, declaring a static member variable as 'private' does not affect the way the linker treats the name, only the way the compiler treats it.) 4. Declaring a member function within a class as 'static' means that it does not operate on any object of that class type, i.e., it has no 'this' pointer. It does, though, have access to all static variables in the class (and its friends). Like (3), its name has class scope. Also like (3), it has "external" linkage. Since it does not have an associated 'this' object, it is almost the same as a free (nonmember) function, except for the scope of its name and the access it has to members of the class (and friends). Despite the overloaded meaning of the 'static' keyword, these are the only distinct uses of it. It might help to keep things straight by thinking of uses (1) and (2) as 'non-auto', and uses (3) and (4) as 'class-shared'. [In my opinion, a different keyword, like 'shared', could have been chosen for uses (3) and (4) in order to reduce confusion.]