C ++: static keyword

Source: Internet
Author: User

Since the C language, static is a confusing keyword, because it represents a far cry between the two.

The book C and pointer provides a detailed explanation:

  • Used for function definition or variable declaration outside the code block

Modifies the link attribute of an Identifier from external to internal. However, the storage type and scope of the identifier are not affected. The declared functions or variables can only be accessed in the declared source files.

  • When static is used for variable declaration within a code block

IndicatesStorage TypeFrom automatic variables to static variables, but the Link Attributes and scope of variables are not affected. Variables declared in this way are created before the program is executed and exist throughout the entire execution period of the program. Instead of being created each time the code block starts to be executed, the variables are destroyed after the code block is executed.

In C ++, static has two more scenarios:

  • Defining static data members of a class

Indicates the member.Belongs to this classInstead of a specific object. A shared memory area is opened for all objects in this class, which provides a communication method for all objects in this class and is hidden inside the class, this avoids the global variable defect: it can be modified by anyone and may cause name conflicts.

Static data members must be defined outside the class (Inline is not allowed) and can only be defined once. Therefore, it is usually placed in a class implementation file. The storage region is allocated only when the variable is defined. If a static data member is declared only in the class definition but not defined, the connector reports an error.

  • Define static member functions of a class

Indicates that the member belongs to this class, rather than a specific object.

Generally, the current object address (this) is implicitly passed to the called function, but the static member function is not related to a specific object and does not have the this pointer, the non-static members in the object cannot be found.Only static data members can be accessed in static member functions, and only other static member functions can be called.. In addition, for all callers of a static member function, only one copy of the local variables defined in the static member function is allowed.

Static member functions cannot be declared as Const. Because static members are not part of any object, declaring a member function as const does not modify the object to which the function belongs.

Static member functions cannot be declared as virtual functions.

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.