c++static keyword Usage

Source: Internet
Author: User
There are three kinds of static functions: restricting variables or function scopes, persisting variable contents, and initializing the default to 0

1. A global function or variable modified by the static keyword has a file scope that is visible only in the current file.

2. Variables modified by static are stored in the static storage area, and the life cycle is defined until the end of the program. For local variables, the static variable persists even after the function exits, but it cannot be accessed. In addition, static modified variables will only be initialized once in a lifetime (this is often used as a small question for interviewing, and should be noted).

3, it is because the static modified variables will be stored in the storage area, so this one. Because static storage variables are initialized to 0 by default.

Second, in addition, static can also modify C + + class member variables and class member functions

1. A static member function of a class is an object of the entire class rather than a class, so it does not have the this pointer, which causes it to access only the static data and static member functions of the class.

2. The static member function does not contain this pointer, so it can be used as a callback function. However, in order to access a member variable of a class, you can pass the object's this pointer as an argument to the callback function.

3. Static member functions cannot add the static keyword decoration when the class definition is in vitro, because the member function is a class scope, and the static modification in the class is used to extend it to the file scope, so it is unreasonable.

4. Static member variables are not initialized in constructors like normal member variables, but are initialized in the implementation file of the class, that is, they must be initialized in the. cpp file, otherwise there will be errors when the program is linked, redefined, and no more static keyword adornments are required for initialization.

5. The const member variable of the static modifier can be initialized when it is defined in the class.

C + + 's syntax is too complicated to be clear at a sudden. There may be some other rules, if there is, please leave a message to me, thank you.

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.