The role of keyword static in C + +

Source: Internet
Author: User
Tags define local

Tag: It differentiates the data area static class initial stat other stack space

1, define local static variables, stored in the global data area of the static variable area. Initialization is automatically initialized to 0, and its scope is a local scope, and when the function or statement block defining it ends, its scope ends.
2, static when defining global variables, this global variable can only be accessed in this file, cannot be accessed in other files, even extern external declarations can not.
3, static defines a function, then this function can only be called in this file, cannot be called by other files.
(1) When you do not want to be released, you can use the static modifier. For example, an array of modifier functions stored in the stack space. You can use the static modifier if you do not want this array to be released at the end of a function call
(2) Take into account the data security (when the process wants to use the global variable should consider the use of static)

4, the definition of static data members, has the following characteristics:

(1) Memory allocation: In the program's Global data area assignment

(2) initialization and definition: Static data member definitions are allocated space, so they cannot be defined in a class declaration

5. Define static member functions.

A static member function is associated with a class and is not associated with the object of the class. Static member functions cannot access non-static data members.

The role of keyword static in C + +

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: 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.