C ++ static keywords

Source: Internet
Author: User

C ++ static keywords

C ++ static keywords

 

 

Static is a common modifier in C ++. It is used to control the storage and visibility of variables.

 

 

Process-oriented static

 

-Static global variables

Add the keyword static before the global variable, which is defined as a static global variable.

# Include
  
   
Usingnamespace std; void fn (); // declare the function static int n; // declare the static global Variable void main () {n = 20; // assign the initial value printf ("% d", n) to n; // output the value of n fn (); // call the fn function} void fn () {n ++; // The value of n plus one (n = n + 1) printf ("% d", n); // output the value of n}
  

 

Static global variables: uninitialized static global variables are automatically initialized to 0 by the program (the values of the automatically declared variables in the function body are random unless they are explicitly initialized, the automatic variables declared in the function body will also be initialized to 0); static global variables are visible in the entire file that declares them, but not outside the file;

 

Note: differences between global variables and Global static variables
  • 1) global variables are global variables that are not explicitly modified using static. global variables are externally linked by default. The scope is the global variables defined in a file throughout the project, in another file, global variables can be used through the Declaration of the extern global variable name.
  • 2) Global static variables are global variables explicitly modified with static. The scope is the file where the variable is declared. Other files cannot be declared using extern.

    -Static local variables
    #include
        
         void fn();void main(){    fn();    fn();    fn();}void fn(){    static int n=10;    printf("%d",n);    n++;}
        

     

    Static local variables solve the problem: a variable is defined in the function body, and stack memory is allocated to the local variable whenever the program runs the statement. However, as the program exits from the function body, the system will reclaim the stack memory and the local variables will also become invalid. Or you need to save the variable value between two calls.

     

    Some people may say, why don't we get cooler globally? If a global variable is used, the variable no longer belongs to the function itself, and is no longer only controlled by the function, causing inconvenience to program maintenance.

     

    Static local variables can solve this problem. Static local variables are stored in the global data zone, instead of in the stack. Each value is kept to the next call until the next value is assigned.

     

    -Static Functions
    # Include
        
         
    Static void fn (); // declare the static function void main () {fn ();} void fn () // define the static function {intn = 10; printf ("% d", n );}
        

     

    The difference between a static function and a common function is that it can only be seen in the file where it is declared and cannot be used by other files. Functions with the same name can be defined in other files without conflict;

     

     

    Static

     

    -Static data member

    Add the keyword static before the declaration of the data member in the class. The data member is the static data member in the class.

    # Include
        
         
    ClassMyclass {public: Myclass (inta, intb, intc); void GetSum (); private: int a, B, c; static int Sum; // declare static data members }; intMyclass: Sum = 0; // defines and initializes the static data member Myclass: Myclass (inta, intb, intc) {this-> a =; this-> B = B; this-> c = c; Sum + = a + B + c;} voidMyclass: GetSum () {cout <"Sum =" <
         
        

     

    Feature: no matter how many objects are created in the class, the static member only keeps one copy in the memory. This copy is shared by all objects in the class. That is to say, static data members are shared by all objects in the class. For multiple objects in this class, static data members only allocate memory once for all objects. Therefore, the values of static data members are the same for each object, and their values can be updated;

     

     

    Static data members follow the public, protected, and private access rules like common data members;

     

     

    Two essential parts of static members: In-class declaration and out-of-class initialization

     

    Static data member Initialization is different from general data member initialization. The static data member initialization format is:
    <; Data type> <; Class Name >:<; static data member name >=<; value>

    Static data members of a class can be accessed in two forms:

    <; Class Object Name>. <; static data member name> or <; class type name >:<; static data member name>
    -Static member functions
    Class Myclass {public: Myclass (int a, int B, int c); static void GetSum (); // declare the static member function private: int a, B, c; static int Sum; // declare static data member} int Myclass: Sum = 0; // define and initialize static data member Myclass: Myclass (int a, int B, int c) {this-> a = a; this-> B = B; this-> c = c; Sum + = a + B + c; // non-static member functions can access static data members} void Myclass: GetSum () // static member function implementation {// cout <

     

    Static member functions can be defined in the class. It can also be declared in the class and defined outside the class. When defining a non-class definition, you cannot use the static keyword as the prefix.

     

    • Call Method

      -Class 1: static member functions

      As shown above

       Myclass::GetSum();

      -2 object. static member function

      As shown above

      M.GetSum();

       

      Note the following before calling a static member function through an object:

       

      1. The prerequisite for accessing static member functions through objects is that the object has been submitted. 2. The access permission for static member functions is the same as that for common member functions. 3. static member functions can also omit parameters and use the default parameter value, and heavy load.

       

      The difference between static member functions and common functions and the use of member functions: because a static member function has only one copy in the class, the members who access the object need to receive some idle: static member functions can directly access static members described in the callback class, but cannot directly access non-static members in the callback class. to access non-static members, you must obtain the corresponding object by passing parameters, and then access it through an object. Note that static members do not have the this pointer because they are independent of objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.