Summarizes the static keyword [C ++] in C ++

 

 C ++ is static in two ways: process-oriented programming of static and object-oriented programming is static. The former is applied to common variables and functions, it does not involve the like; static action described in the latter class.

1. Process for the design of static

1.1 static global variables

Before global variable with the keyword static, the variable is defined as a static global variable . Let's give an example of static global variables, as follows:

. 1  // Example. 1 
2 #include <, iostream.h>
 . 3  void Fn ();
 . 4  static  int n-; // define static global variable 
. 5  void main ()
 . 6  {
 . 7     n-= 20 is ;
 . 8     COUT n-<< << endl ;
 . 9     Fn ();
 10  }
 . 11  
12 is  void Fn ()
 13 is  {
 14     n-++ ;
 15     COUT n-<< << endl;
 16 }

Static global variables have the following characteristics:

  • Global variables are allocated in the data memory area;
  • Uninitialized static global variables are automatically initialized to 0 (the default initialization) ( value of the automatic variable is random, unless it is explicitly initialized );
  • Static global variables in a statement its entire file is visible, in addition to the file is not visible; 

Static variables are allocated in the global data area of memory, including static local variables will be mentioned later. For a complete program, in the distribution of memory as follows: 
code region
global data
heap area
stack area

Dynamic data generated by the new general procedures stored in the internal region of the stack function automatically variable in the stack area. With automatic variables tend to withdraw from functions and free up space, static data (even internal functions static local variable) is also stored in the global data area. Data in the global data area and will not exit the function and free up space.

Careful readers may find the code in the Example 1 in the " static int the n-; // define the static global variable " to "int n; // define global variables" . Program still operating normally. Indeed, the definition of global variables can be shared variable in the file, but the definition has the following advantages static global variables:
• static global variables can not be used in other documents;
• other variables can be defined in the same file name, not conflict ;

You can read the above sample code as follows:

. 1  // Example 2
 2  // the File1 
. 3 #include <, iostream.h>
 . 4  void Fn ();
 . 5  static  int n-; // define static global variable 
. 6  void main ()
 . 7  {
 . 8     n-= 20 is ;
 . 9     COUT << << n- endl;
 10     Fn ();
 . 11  }
 12 is  
13 is  // the File2 
14 #include <, iostream.h>
 15  extern  int n-;
 16  void Fn ()
 . 17  {
 18 is    n++;
19    cout<<n<<endl;
20 }

Compile and run Example 2, you'll see that the above code can be compiled, but the run-time error, respectively.

Try the  "static int n; // define a static global variable" changed to  "int n; // define global variables"
compile and run the program again, carefully understand "global variables" and "static global variable" distinction.

1.2 Static local variables

Before local variables, with the keyword static, the variable is defined as a static local variable. Let's give an example of a static local variable, as follows:

 1 //Example 3
 2 #include <iostream.h>
 3 void fn();
 4 void main()
 5 {
 6    fn();
 7    fn();
 8    fn();
 9 }
10 void fn()
11 {
12    static n=10;
13    cout<<n<<endl;
14    n++;
15 }

Why do we need a static local variable

Typically, the body of the function definition of a variable, to run whenever the statement is assigned to the local variable stack memory. But as the program exits the function body, the system will recover the memory stack, local variables accordingly invalid. But sometimes we need to save the values ​​of variables between the two calls. The general idea is to define a global variable to achieve. But in that case, the variable is no longer part of the function itself, not only by the control function, to the maintenance program of inconvenience.

Static local variables can just solve this problem. Static local variables stored in the global data area, and not stored in the stack, each value remains until the next call until the next assigns a new value.

Static local variable has the following characteristics:
• The variable allocates memory in the global data area;
• a static local variable in a program to be executed first initialized, that is, after the function call will not be initialized when declared at the object;
• a static local variable in general in a statement at initialization, if not explicitly initialized, it will be automatically initialized to 0;
•  it is always resides in the global data area until the end of the program run. But its scope is local scope, as it is defined at the end of the statement or function blocks, which ended with a scope;

1.3 static function

  Plus the static keyword before the return type of the function, the function has been defined as static functions. Different static function and normal function, it can only declare its file which can be seen, can not be used by other files.
Examples of static functions:

. 1  // Example. 4 
2 #include <, iostream.h>
 . 3  static  void Fn (); // declare static function 
. 4  void main ()
 . 5  {
 . 6     Fn ();
 . 7  }
 . 8  void Fn () // defines static function 
9  {
 10     int n-= 10 ;
 . 11     COUT n-<< << endl;
 12 is }

Benefits define a static function:
• static function can not be used in other documents;
• other files can define functions with the same name, not conflict;

2, object-oriented static keyword (keyword static class)

2.1 Static data members

With the keyword static before the statement within the class data members, members of the data is static data members of the class. First give an example of a static data member.

. 1  // Example. 5 
2 #include <, iostream.h>
 . 3  class Myclass
 . 4  {
 . 5  public :
 . 6     Myclass ( int A, int B, int C);
 . 7     void GetSum ();
 . 8  Private :
 . 9     int A, B, C ;
 10     static  int the Sum; // declares a static data members 
. 11  };
 12 is  int Myclass :: the Sum = 0 ; // define and initialize static data member 
13 is  
14 Myclass :: Myclass (int a,int b,int c)
15 {
16    this->a=a;
17    this->b=b;
18    this->c=c;
19    Sum+=a+b+c;
20 }
21 
22 void Myclass::GetSum()
23 {
24    cout<<"Sum="<<Sum<<endl;
25 }
26 
27 void main()
28 {
29    Myclass M(1,2, 3 );
 30     M.GetSum ();
 31     Myclass N ( 4 , 5 , 6 );
 32     N.GetSum ();
 33     M.GetSum ();
 34  
35 }

As can be seen, the static data member has the following characteristics:

  • For non-static data members, each object has its own copy of the class. The static data member is treated as a member of the class. Whether objects of this class is defined as how many, static data members and only one copy of the program, the share accessible to all objects of that type. In other words, static data members are common to all objects of that class. A plurality of objects of this class, the static data allocated only one memory member, common to all objects. Therefore, the value of the static data members of each object is the same, its value can be updated;

  • Static data members are stored in the global data area. Static data members to allocate space definition, can not be defined in the class declaration. In Example 5, the statement int Myclass :: Sum = 0; static data member is defined;

  • Static data members and ordinary members of the same data to comply with public, protected, private access rules;

  • Because static data members allocated memory in the global data area, all belong to this class of shared objects, so that it does not belong to a specific class of objects, which is visible when the scope is not generated on the class object, i.e., when no instance of the class, we can operate it;

  • Static data member initialization initialize data members and the general different. Static data member initialization format:

<Data type> <class name> :: <static data member name> = <value>

  • Static data members of the class, there are two forms of access:

<Class object name>. <Static data member name> or <class type name> :: <static data member name>

  • If the static data member permits access (i.e., public members), may be in the program, the above-described data format to refer to a static member;

  • Static data members are mainly used when an individual objects have the same attributes. For example, a deposit, the interest is the same in each instance. Therefore, the interest should be set static data members of the class of deposit. This has two advantages, first, no matter how many deposits class object definitions, data members are shared interest in allocating global data memory area, so save storage space. Second, once the interest needs to be changed, as long as the change once, all objects of interest deposit over the whole change;

  • Compared with global variables, use static data members have two advantages:

  1. Static data members not to enter the global name space programs so that other programs and the possibility of global conflict name does not exist;
  2. The information can be hidden. Private static data members can be members, global variables can not;

2.2 static member function

  As with static data members, we can also create a static member function, it is like all the services rather than the object of a specific class of service. Static member functions and static data members, both internal class implementation, are part of a class definition. Ordinary member functions are generally implies a this pointer, this pointer to the object class itself, because ordinary member functions always belong to specific objects of a specific class of. Typically, this is the default. The function Fn () actually this-> fn ().

However, compared with ordinary, static member functions because it is not associated with any object, and therefore it does not have this pointer. In this sense, it can not access non-static data members belong to the class object, can not access non-static member function, it can only call the rest of the static member function. The following give an example of a static member function.

. 1  // Example. 6 
2 #include <, iostream.h>
 . 3  class Myclass
 . 4  {
 . 5  public :
 . 6     Myclass ( int A, int B, int C);
 . 7     static  void GetSum (); / declare static member functions
 . 8  Private :
 . 9     int A, B, C;
 10     static  int the Sum; // declares a static data members 
. 11  };
 12 is  int Myclass :: the Sum = 0 ; // define and initialize static data members 
13 
14 Myclass :: Myclass ( int A, int B, int C)
 15  {
 16     the this -> A = A;
 . 17     the this -> B = B;
 18 is     the this -> C = C;
 . 19     the Sum + = A + B + C; // non-static member functions can access static data member 
20 is  }
 21 is  
22 is  void Myclass :: GetSum () // static member function to achieve 
23 is  {
 24    // COUT a << << endl; // error code, a non-static data member 
25     cout << " Sum = " << Sum << endl;
26 }
27 
28 void main()
29 {
30    Myclass M(1,2,3);
31    M.GetSum();
32    Myclass N(4,5,6);
33    N.GetSum();
34    Myclass::GetSum();
35 }

About static member functions, can be summarized as follows:

  • Vitro function definition appears in the class can not be static keyword;
  • Between static members can access each other, comprising a static member functions to access the static data members and member functions static access;
  • Any non-static member functions can access static member functions and static data members;
  • Static member function can not access non-static member functions and non-static data members;
  • Because there is no overhead this pointer, so the global functions and static member function of class compared to the previous rate of growth will be a little;
  • (.) Calling static member functions can be accessed by members of the operator, and (->) calling a static member function pointer points to an object class or a class of objects, it may be used directly following format:
    <class name> :: <Static member function name> (<parameter list>)
    static member function calls the class.

 

Reprinted from https://www.cnblogs.com/BeyondAnyTime/archive/2012/06/08/2542315.html

Guess you like

Origin www.cnblogs.com/Fionaaa/p/12639738.html