Usage of the C ++ keyword static

Source: Internet
Author: User

1. Global static variables in the cpp File

Global declaration:

Static int a = 0;

Such static global variables can only be used by the cpp file and cannot be shared by other cpp files.

If static is not declared:

Int a = 0;

All such variables cannot guarantee that a can be shared by other cpp files, nor can it be shared by other cpp files. It is not recommended to use this method. It is best to add extern:

Extern int a = 0;


2. Static local variables

Int f ()

{

Static int a = 1;

}

Static local variables are stored in the static variable area, rather than in the stack. Therefore, the life cycle is the entire program cycle, not the function cycle. Apply for memory only once, and then save the value of last a when the function is called next time.


3. Global static functions in the cpp File

Declare in cpp:

Static int func ();

Like Global static variables in cpp, the function only applies to the cpp file. If you want to call multiple cpp files, put them in the header file and do not add static.


4. Static members of a class

Class

{

Private:

Static int;

}

The static member scope is Class A, and variable a is A shared member variable of Class A, instead of all of an instance. Static member a must be initialized in cpp:

Int A: a = 0; // Note: There is no static modifier here, because static is A declarative keyword.


5. static member functions in the class

Class

{

Private:

Static void func (int );

}

When this function is implemented, the keyword static is not required because static is a declarative keyword;

The static member function of A class is A global function in the category of this class. It cannot access private members of this class. It can be called without an instance. :: func (9 );

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

Static does not have the this pointer.


Summary: in fact, the static principle is that it is in the static variable area of the memory, so the life cycle is different. Second, he applies only once, so it will not be overwritten.

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.