C + + static notes

Source: Internet
Author: User

"Turn" http://www.cnblogs.com/zi-xing/p/4590282.html

The role of Static

  1. In the function body, a variable declared static, the value of which is maintained in the process of the function being called.
  2. Within the module (but outside the function body), a declared static variable can be accessed by all functions within the module, but cannot be accessed by a function outside the module, which is a local global variable
  3. Within the module, a function declared as static can only be called by other functions within the module, which is used within the module that declares it.

In general, static controls the scope of use of variables and functions (Local data and code range control)

The difference between a static global variable and a normal global variable

Both global variables and static global variables are static storage methods. The difference is that the scope of the non-static global variable is the whole source program, when a source program has multiple source files, non-static global variables are valid in each source file, while static global variables limit the scope, only the source file that defines the static global variable is valid, the other source files of the same source program cannot use it. Use static global variables to avoid causing errors in other source files.

In summary: Changing the local variable to a static variable changes its storage mode, and changes its lifetime, changing the global variable to a static variable, changing its scope and limiting its scope of use.

  1. Static global variables differ from normal global variables: Static global variables are initialized only once, preventing references in other file units
  2. Static local variables differ from ordinary variables: Static global variables are initialized only once, and next time based on the last result value.
  3. The difference between a static function and a normal function: The static function has only one copy in memory, and the normal function maintains a copy of each call.

C + + static notes

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.