About the understanding of the static keyword in C + +

Source: Internet
Author: User

The static keyword has different meanings in different locales, or in different contexts of the same language.

1. C language

1.1 variables declared as static have the following three-part characteristics:

1) storage location: stored in static memory

2) Create and scope: A static variable is created before the program runs and always exists throughout the execution of the program.

3) Initialize value

1.2 Types

1.2.1 Static global variables:

1) in-memory location: static storage (static storage is present during the entire program run)
2) Initialize: Uninitialized global static variable is automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless he is displayed initialized)
3) Scope: Global static variables are not visible outside the declaration of his file. Start with the definition exactly to the end of the file.
Benefits:
Benefits of defining global static variables:
<1> will not be accessed by other files, modify
<2> other files can use variables of the same name, and no conflicts will occur.

1.2.2 Static Local variables:

1) in-memory location: Static Storage Area
2) Initialize: Uninitialized global static variable is automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless he is displayed initialized)
3) Scope: The scope is still a local scope, and when the function or statement block defining it ends, the scope ends.

1.2.3 Static functions

the definition and declaration of a function is extern by default, but the static function is only visible in the file that declares him and cannot be used by other files.
Benefits of defining static functions:
1) functions with the same name can be defined in other files and no conflicts will occur
2) Static functions cannot be used by other files.

2. In C + +

2.1 Static data members

Precede the declaration of a data member within a class with the keyword static, which is a static data member within the class.

1) storage location: Static data members are stored in the global data area

2) Initialize:

3) static data members are treated as members of the class. Regardless of how many objects of this class are defined, static data members have only one copy in the program and are shared by all objects of that type.

4) Access mode: Class object name >.< static data member name or class type name >::< static data member name

5) You can also access static data members when no instances of the class are generated

Because static data members allocate memory in the global data area, all object shares of this class are shared, so it does not belong to a particular class object, its scope is visible when no class object is produced, that is, we can manipulate it without producing an instance of the class;

2.2 Static member functions

Characteristics:

1) static member functions and static data members can be accessed arbitrarily by non-static member functions;
2) static member functions cannot access non-static member functions and non-static data members;

3) 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;
4) Call a static member function, which can be accessed using the member access operator (.) and (-) call a static member function for an object of a class or pointer to a class object, or you can use the following format directly:
Class name >::< static member function name > (parameter table)

About the understanding of the static keyword in C + +

Related Article

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.