Static keyword __static

Source: Internet
Author: User
Three features summary : share :

allocating memory in the global data area will only have one copy.
shared by function (where static local variables are called multiple times)
To be shared by (all) objects of the class in which the static method or variable resides :

Static Global variables can only be used by the file in which they reside
Static local variables can only be used by the function
A static member variable or function complies with the default value of the 3p rule (public, protected, private):

Uninitialized static variables are automatically initialized by the program to 0 process-oriented (c + +) static global variables

Global variables are scoped to the entire project, global variables defined within one file, and another file can use global variables by extern+ the declaration of global variable names . For security and encapsulation considerations, we do not want the global variables of this file to be used by other files. Static global Variables

The main feature of a static global variable is the hidden (/ω\), whose scope is to declare the file where the variable resides, and other files that use an extern declaration cannot be used. global variable test code:

Run Result:

A

As you can see, global variables can be invoked with extern declarations by other files. static global variable test code:

Run Result:

B

You can see that you can define variables in other files that have the same name, and there is no conflict. Static Global variables also have the following additional features:

The variable allocates memory in the global data area ;
Uninitialized static global variables are automatically initialized by the program to 0. Static Local Variables

The main characteristics of static local variables are embodied in shared ( ̄▽ ̄)/, which is called durability better understood. That is, when the function exits, it still exists in memory until the function is called again, using the value of the static local variable after the last call. static local variable test code:

Run Result:

9 4 3 2 1 0 9

As you can see, the static local variable a in the function remembers the last execution value, and the next time the function is executed, the value at the end of the last call is used.

The function of the local variable A and the function of the variable A does not affect, but this is not static credit, remove static also will not have error, but can not remember the last time the function was called local variable a value. static Local variables also have the following additional features:

Static local variables allocate memory in the global data area , always reside in the global data area until the program is finished;

Static local variables are initialized for the first time when the program executes to the declaration of the function, and subsequent function calls are no longer initialized;

Static local variables are typically initialized at the declaration and, if not explicitly initialized, are automatically initialized to 0by the program;

Its scope is within this function and cannot be invoked outside of the function . static Functions

By adding the static keyword before the return type of the function, the function is defined as a static function . The difference between a static function and a normal function is similar to the difference between a static global variable and a global variable. Normal functions can be invoked by other files as extern , and static functions can only be invoked by the file in which they reside . Object-oriented static data members

First look at a piece of code:

Run Result:

1 1 3 3 3

From the above we can see that the main feature of static data members is shared , all objects of Class A a1, a2** share * * Same number. When A2 changes the value of number, the value of A1 number changes.
Before the object of Class A appears, we can access number by A::number . static data member features:

Static data members are stored in the global data area , and there is only one copy in the program, and all objects of that class are shared .

Static data member definitions are allocated space, so they cannot be defined in class declarations .

Static data members comply with public,protected,private access rules like regular data members.

Static data members do not belong to a particular object, and when no instance of the class is generated, we can use it in the following ways (it needs to be public).
< class name >::< static data member name >//c++
< class name >.< static data member name >//java

Static variables can be referenced directly through the class name , or static variables can be referenced through the instance name , but it is preferable to use the former, which is easy to confuse static and general variables . A static variable is associated with a class, and all instances of the class share a static variable.

The format for static data member initialization is:
< data type > < class name >::< static data member name >=< value > Static member functions


for static member functions , you can summarize the following points:

non-static member functions can access static member functions and static data members ;

static member functions do not have access to non-static member functions and non-static data members ;

All objects of this class are shared and do not belong to any one object and cannot use the This keyword.

To invoke a static member function , you can use the following format directly:
< class name >::< static member function name > (parameter list)//c++
< class name >.< static member function name > (parameter list)//java

static functions can also be referenced through instance names , but it is preferable to use the former, which is easy to confuse static functions with general functions . A static function is associated with a class, and all instances of the class share a static function.

You cannot define a static function as a virtual function . (c + +)

function definitions that appear outside of the class body cannot specify keyword static(c + +)

Because there is no extra overhead for this pointer, static member functions have a slight increase in speed compared to the global functions of a class;

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.