[C ++] What are the static keyword usage? What are its main role is?

static keyword usage:

1) The global variable modified to static global variables

Stored in the static storage area, there has been during the entire program run

Static global variables in a statement it is not visible outside the file , as long as it is declared in the file is visible, while the average global variables are visible to all files

2) The local variable is modified to a static local variable

In the static storage area

Scope remains local scope, but when a local variable goes out of scope, and did not destroy, but are still stored in memory, but is temporarily not accessible until the function is called again, and the value is unchanged , it is initialized only once

#include <stdio.h>
 int Fun () 
{ 
    static  int COUNT = 10 ; // the first time through the function, the variable is initialized to a 10! 1 and then decremented after each access to the function, A 
    return count--; // will not be re-initialized, only the operation performed from minus 1; before static invention, to achieve the same function, only global variables can be used: 

} 
int COUNT = . 1 ;
 int main ( void ) 
{ 
    the printf ( " global \ T \ tlocal static \ n- " );
     for (; COUNT <= 10 ; ++ COUNT) 
        the printf ( " % D \ T \ T% D \ n- " , COUNT, Fun ());
     return 0 ; 
} 
/ * 
program running results: 
Global local static 
. 1 10 
2. 9 
. 3. 8 
. 4. 7 
. 5. 6 
. 6. 5 
. 7. 4 
. 8. 3 
. 9 2 
10. 1 
* /

3) The normal function modified to static function

Static function is only visible in a statement on its file , so it will not cause conflicts with other functions of the same name in the cpp

4) the class member variables modified to static member variables

Static member variables are part of the class, rather than belonging to the object , static member variables can share data between multiple objects, and static member variables will not undermine the principle of hidden, multiple objects, the static member variables storing only one, common for all objects

5) will be modified to a member function class static member function

Static member functions also belong to a class, and not to object to note that non-static data members can not be referenced directly in the realization of a static member function of the class, but can directly reference a static member, if you must reference the non-static members, it can be referenced by an object


the role of the static keyword:

1) Scope Hide

When compiling multiple files, not all of the prefix plus static global variables and functions have global visibility, can be used as a prefix static functions and variables, in terms of function, the role of static in hiding only

2) maintain a lasting variable content

Static data stored in the variable region will be completed initialization program started running, and only time initialization

3) The default is initialized to 0 (static variable)

4) static member functions and static member variables belong to the class, only one copy of all the objects , it can not be static function to virtual function (virtual function is achieved polymorphism, rather than static members belong to the class object is no more the concept of state)


Guess you like

Origin www.cnblogs.com/yinbiao/p/11532540.html