Summary of static usage in C + +

Source: Internet
Author: User
Tags require valid

 Full usage of static

To understand the static, you must first understand the other relative keywords, many people may not know that there is a keyword, that is, auto, in fact, we usually declare the use of static modified variables, are auto, because it is the default, Just as short and long always default to int; we usually declare a variable:

int A;

string S;

  is actually:

auto int A;

Auto string s;

and the declaration of the static variable is:

static int A;

static string S;

This seems to be more conducive to understanding the auto and static is a pair of keywords, like private,protected,public;

For the static does not understand, in fact, it is not understanding of auto, because it is more general, some things you use every day, but not necessarily on behalf of you really understand it; the meaning of auto is to control the life cycle of the variable by the program, usually the variable is allocated when it enters its scope, is released from its scope; the static is not auto, the variable is allocated when the program is initialized, until the program exits before it is released; the static is assigned to release variables according to the lifecycle of the program, rather than the variable's own life cycle; So, examples like this:

void Func ()

{

int A;

static int b;

}

Each time the function is called, the variable A is new, because it is allocated when it enters the function body, it is released when it exits the function body, so multiple threads calling the function have their own independent variable a because it is always reassigned; and variable B doesn't matter whether you use the function or not, is assigned when the program is initialized, or when it is first executed to its declaration (different compilers may be different), so when multiple threads call the function, they always access the same variable B, which must be noted in multithreaded programming!

Full usage of static:

1. Static members of a class:

Class A

{

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, it is a global variable within the scope of the class, but it is also understood to be a global variable named A::s_value, except that it has a class security attribute; The reason is simple because it is allocated at the time the program is initialized, so it is assigned only once, So it is common to use;

The static members of a class must be initialized, and the same is true. Because it is allocated at the time of initialization of the program, it must have initialization, the class is only declared, in the CPP is initialized, you can put a breakpoint on the initialization code, before the program executes main the first statement will go there first If your static member is a class, then it calls to its constructor;

  2. Static functions of a class:

Class A

{

Private

static void func (int value);

};

The implementation also does not require static modification, because static is a declarative keyword;

A static function of a class is a global function within the scope of the class, which cannot access the private members of the class, can only access static members of the class, and does not require instances of the class to be invoked; in fact, it is a global function that adds access to the class: void A::func (int);

Static member functions can inherit and overwrite, but cannot be virtual functions;

  3. Global variables that are valid only within a CPP:

Declare in the global scope of the CPP file:

static int g_value = 0;

The meaning of this variable is valid within the CPP, but other CPP files cannot access the variable, and if two CPP files declare global static variables of the same name, they are actually independent of two variables;

If you do not declare a global variable with static:

int g_value = 0;

Then there is no guarantee that this variable will not be shared by other CPP. There is no guarantee that it will be shared by other CPP, because to have multiple CPP share a global variable, it should be declared extern (external), or it may compile to report that the variable is repeatedly defined; The use of this global variable is not clear;

If you declare in a header file:

static int g_vaule = 0;

A global variable is created for each CPP that contains the header file, but they are all independent, so it is not recommended to do so, as it is not clear how the variable needs to be used, because it creates only a set of variables with the same name and different scopes;

Here, by the way, how to declare any global variable that the CPP can share, declared as extern in the header file:

extern int g_value; Note, do not initialize the value!

You can then initialize (once) in any of the CPP that contains the header file:

int g_value = 0; As an initialization, do not extern decoration, because extern is also declarative keywords;

Then all CPP files containing the header file can access the same variable using the name G_value;

  4. Global functions that are valid only within a CPP:

  Declare in CPP:

static void Func ();

The implementation of a function does not require a static modification, so this function can only be used within this CPP and will not cause conflicts with a function of the same name in other CPP; the same problem as if you don't use static is the 3rd; Do not declare static global functions in header files. Do not declare a non static global function in CPP, if you want to reuse the function in multiple CPP, refer to its declaration in the header file, otherwise in the CPP internal declaration need to add static modification; In C language this is important!

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.