Static1. local variable
// The life cycle of the variable is not only after entering the function, but also void Foo () {static int nvar = 0; printf ("Var = % d \ n ", ++ nvar );}
2. protect local variables
Static int g_nvar; // g_nvar can only be used in this file. Other files cannot access this variable through extern.
3. static member variables
// This variable does not belong to the object, but belongs to this class Foo {public: static int ncount ;}; int FOO: ncount = 0; int main () {cout <FOO: ncount <Endl; return 0 ;}
4. static member functions
// This function does not belong to an object, but belongs to this class Foo {public: static void showclasstag () ;}; void FOO: showclasstag () {cout <"class foo" <Endl;} int main () {FOO: showclasstag (); Return 0 ;}
Const1. define a constant
const int nItem = 0;const int* pnItem = &nItem;
2. Protection variables are not used externally
// File1.cconst int g_nlocalvar = 100; int g_nvar = 100; // file2.cextern const int g_nlocalvar; // illegal declaration of extern int g_nvar; // valid
Extern1. the object referenced in the declaration has been defined.
extern int g_nFoo;int main(){g_nFoo++;cout << g_nFoo << endl;return 0;}int g_nFoo = 0;
2. declare that the variable can be used externally
// File1.cextern const int g_nitem; int main () {cout <g_nitem <Endl; return 0 ;}// file2.c // If extern is not added here, extern const int g_nitem = 100;