Static Keyword Summary

Source: Internet
Author: User

A-static local variable.
1. The function of the static variable within the scope of the function, the variable is initialized in memory only once, the next time the use of the previous value is still maintained, no longer the initialization of the work. Because the static variable is saved in the global data area instead of the stack, it remains valid until the next use. Because it is stored in the global data area, its effective range is within the local function, but the life cycle is accompanied by the end of the life cycle of the whole process.

Two-Global static variables
1) The initialized global static variable is not displayed, and the system initializes the corresponding default according to the type of the static global variable.

2) A static global variable that declares its entire file is visible, beyond the scope of the file is invisible, and since it is invisible, a variable with the same name can be defined in another file. Static variables are stored in the global data area. While global non-static variables are visible to the entire project, you can use extern in an external file using that global variable.

Three-static functions
1) Other files can define functions with the same name, and no conflicts will occur.
2) Static functions cannot be used by other files.

Four-static member variable
1) for non-static data members, each object has its own copy. Static member variables, regardless of how many objects are defined, will have only one copy in the program, meaning that all objects can access the object.
2) Static members have access rules public,protected,private for ordinary member variables.
3) Initialization of static member variables:
A static data member must exist at the beginning of a program run. Because a function is called in a program run, a static data member cannot allocate space and initialization within any function. In this way, its spatial allocation has three possible places, one is the header file as the outer interface of the class, there is the class declaration, and the second is the inner implementation of the class definition, there is the member function definition of the class, and the third is the global data declaration and definition in front of the main () function of the application.

Five-static member functions
1) The function definition appearing outside the class cannot specify the keyword static;
2) Static members can access each other, including static member functions to access static data members and access static member functions;
3) static member functions and static data members can be accessed arbitrarily by non-static member functions;
4) Static member functions cannot access non-static member functions and non-static data members;
5) because there is no additional overhead for this pointer, the static member function has a slight increase in speed compared to the global function of the class;

Advantage
Can save memory because it is public to all objects, so for multiple objects, static data members are stored only one place for all objects to be shared. The value of a static data member is the same for each object, but its value can be updated. As long as the value of the static data member is updated once, it is guaranteed that all objects access the same value after the update, which can improve time efficiency.

Static Keyword Summary

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.