How to use static in C ++? Summary of C ++ static usage

Source: Internet
Author: User

  
What are the usage of static in C ++? I believe many friends who are learning C ++ will ask this question. Today, the Helper house has compiled four static usage methods in C ++. If you are interested, you can take a look.

To understand static, you must first understand another relative keyword. Many people may not know this keyword, that is, auto. In fact, we usually declare variables without static modification, all are auto, because it is default, just as short and long are always int by default.

We usually declare a variable:

Int;

String s;

Actually:

Auto int;

Auto string s;

The static variable declaration is:

Static int;

Static string s;

This seems to be more conducive to understanding that auto and static are paired keywords, just like private, protected, and public;

I don't understand static, but I don't understand auto because it is more general. Some things you use every day, but it doesn't necessarily mean you really understand it; auto means that the program automatically controls the life cycle of the variable. It usually means that the variable is allocated when it enters its scope and is released when it leaves its scope; static is not auto, and variables are allocated during program initialization until the program exits.

That is, static is used to allocate and release variables according to the life cycle of the program, rather than the life cycle of the variable itself. Therefore, for example:

Void func ()

{

Int;

Static int B;

}

Every time this function is called, variable a is new because it is allocated when it enters the function body and released when it exits the function body. Therefore, multiple threads call this function, each has its own independent variable a, because it is always re-allocated. Variable B is allocated during program initialization no matter whether you use this function or not, or it is allocated when the declaration is executed for the first time (different compilers may be different), so when multiple threads call this function, they always access the same variable B, this is also important in multi-threaded programming!

Static usage 1. static members of a class:

Class

{

Private:

Static int s_value;

};

It must be initialized in cpp:

Int A: s_value = 0; // note that there is no static modification here!

A static member of A class is A shared member of all instances of the class, that is, A global variable in the category of the class. It can also be understood as A global variable named A: s_value, it only has class security attributes. The principle is very simple, because it is allocated during program initialization, so it is shared only once;

Static members of a class must be initialized, and the same is true because they are allocated during program initialization, so Initialization is required. Classes are only declared and initialized in cpp, you can place a breakpoint on the initialization code. the breakpoint will be reached before the program executes the first statement of main. If your static member is a class, the constructor is called;

Static usage 2. static functions of the class:

Class

{

Private:

Static void func (int value );

};

Static modification is not required during implementation, because static is a declarative keyword;

Static functions of a class are global functions in the category of the class. They cannot be private members of the class, but only static members of the class can be called without class instances. In fact, it is the global function that adds class access permissions: void A: func (int );

Static member functions can be inherited and overwritten, but cannot be virtual functions;


Static usage 3. Only valid global variables in cpp:

Declare within the global scope of the cpp file:

Static int g_value = 0;

This variable is valid in this cpp, but other cpp files cannot access this variable. If two cpp files declare Global static variables with the same name, they are actually two independent variables;

If you do not use static to declare global variables:

Int g_value = 0;

Therefore, it cannot be guaranteed that the variable is not shared by other cpp instances, nor can it be shared by other cpp instances, because multiple cpp instances must share one global variable, it should be declared as extern (external); it is also possible that the compilation will report that the variables are repeatedly defined; in short, such writing is not recommended, and the usage of this global variable is not clear;

If you declare in a header file:

Static int g_vaule = 0;

A global variable will be created for each cpp containing the header file, but they are all independent. Therefore, we do not recommend this method. We do not know how to use this variable, because only a group of variables with the same name and different scopes are created;

Here, by the way, how to declare all the global variables that cpp can share and declare them as extern in the header file:

Extern int g_value; // do not initialize the value!

Then initialize (once) in any cpp containing the header file:

Int g_value = 0; // do not modify extern during initialization, because extern is also a declarative keyword;

Then, all cpp files containing the header file can use the g_value name to access the same variable;


Static usage 4. Global functions that are only valid in cpp:

Declare in cpp:

Static void func ();

The implementation of the function does not require static modification, so this function can only be used in this cpp, and it does not conflict with functions of the same name in other cpp; the problem is the same as that caused by not using static. Do not declare static global functions in the header file or non-static global functions in cpp, if you want to reuse this function in multiple cpp programs, you should mention its declaration to the header file. Otherwise, static modification is required for the cpp internal declaration. This is important in C!

The static usage in C ++ is just like this. Mastering these four usage methods can greatly improve the efficiency of C ++.

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.