Static in C \ C ++: process-oriented static: in the process-oriented design of c and c ++, when the static keyword is added before a global variable, the variable can be defined as a static global variable, such as static int a. What are the characteristics of static global variables in c and c ++: 1. allocate memory for variables in the global data zone (local variables are in the stack, and variables dynamically allocated through new and malloc are in the heap ); 2. uninitialized global variables are automatically initialized to 0 by the program. 3. Static global variables or functions are visible in the file where they are declared and invisible outside the file; (can play a protective role) Let's talk about the third point. If we only declare a common global variable a in a header file, when we reference this header file, we can also use the extern keyword to reference this variable to the current file. However, if you add the static keyword when declaring the variable, therefore, global variable a is invisible to any other files. Similarly, if a function is defined in static mode, the function is only visible in the file that defines it. Object-oriented static: In object-oriented design, pay attention to the following points when using static: 1. The declaration of static member functions should be added with the static keyword in the class, but it is not required for implementation outside the class; 2. The static member function does not have the this pointer, so do not show or implicitly reference this pointer in the static member function, this will cause errors during compilation. Therefore, do not try to initialize static member variables in the constructor. Let's take a look at the object-oriented static example below: first define a header file account. h. Define a class in the header file: # pragma onceclass Account {public: static void reiseInterest (double var); static double interest () {return dInterest ;} // provides the implementation in the class. The statement is simple and does not have recursion. It is the same as the implementation outside the class and declared as the inline function. All functions are inline functions: private: static double dInterest;}; double Account:: dInterest = 5; // static data member initializes inline void Account: reiseInterest (double var) {dInterest + = var;} And then tests in the source file: # include <iostream> # I Nclude "account. h" using namespace std; int main () {// double Account: dInterest = 5; // error !! The static keyword limits the field of view !! Cout <"The initial interest is" <Account: interest () <endl; Account ac1; Account ac2; ac1.reiseInterest (18); ac2.reiseInterest (17 ); cout <"The current interest is" <Account: interest () <endl; return 0;} The running result is as follows: