### Static usage

Source: Internet
Author: User

### Static usage
#### Static in process-oriented programming
##### 1. Static global variables
Memory is allocated in the global zone, automatically initialized to 0, and invisible outside the file that declares it.
##### 2. Static local variables
The memory is allocated in the global zone, the automatic Initialization is 0, and the life cycle starts from the declaration to the end of the program, but its scope is within the declared function. That is, only this function can use static local variables, which is equivalent to the global variables dedicated to this function.
##### 3. Static Functions
Add the static function before the function name to define the static function, which can only be used in the file that declares it.

#### Object-oriented static
##### Static data member: Add static before the declaration of the data member in the class, that is, declare the static data member in the class.
1. Non-static members each object has its own copy. Static data members have only one copy in the program, which is shared by all objects in the class. But it does not belong to a specific class and can be manipulated without any class instances.
2. stored in the global data zone. Because space needs to be allocated during definition, it cannot be initialized in class declaration. During off-class initialization, the static keyword is not required during initialization. Format: <data type> <class name >:< static data name >=< value>
3. Although public, protected, and private access permissions are also followed, they cannot be accessed outside the class. There are two formats for accessing static members: <Class Object Name>. <static member name>. If the static member is public, you can <class name >:< static data Name>
4. static data members are mainly used when each object has the same attribute. One is to save storage space, and the other is to modify the storage space in one place. For example, interest in deposits.
5. Compared with global variables, static data members have two benefits. First, it does not enter the global namespace of the program and does not conflict with other global variables. Second, it can implement information hiding and can be defined as private, while global variables cannot.

##### Static member functions
Similar to static data members, static member functions are not a specific object service in the class, but all services of the class.
1. The common member function hides the this pointer to the object by default, while the static member function does not have this pointer. Therefore, it cannot invoke non-static data members and non-static member functions of class objects, and can only call static member functions.
2. Static members can access each other, including static data members and static member functions.
3. Because there is no additional overhead of this pointer, its speed is slightly higher than the global function of the class.
4. class objects can be used. alternatively, you can directly call static member functions, or access them when no class object exists: <class name >:< member function name> (<parameter table>)

### Static usage

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.