Summary of static keywords

Source: Internet
Author: User

Static local variables have the following features:
1. The variable is stored in the static storage area and will not be destroyed until the running of the program ends. But its scope is local scope. When the function or statement block defining it ends, its scope ends.
2. Block-scope static variables: When you first enter the function defining the static variable, construct the variable. When you enter the function, no longer construct the variable.
3. Static local variables are generally initialized at the Declaration. If Initialization is not displayed, the program will initialize them to 0 by default.

Static global variables have the following features:
1. This variable is stored in the static storage area and is partial to the source file. Strictly speaking, the scope should be defined from the beginning to the end of this file.
2. Initialization: If Initialization is not displayed, the program initializes it to 0 by default.
3. Note: The const constant declared under the file scope is static by default.

Static functions have the following features:
1. The static function is partial to the source file and can only be used in the source file (this is the difference from the common function)
2. Note: The inline Function declared in the file scope is static by default.

Static data members have the following features:
1. Because its static data member space is allocated in the static storage area and is shared by all objects of this class, it belongs to the class and does not belong to a specific object,
The scope is visible when no class object is generated, that is, when no class instance is generated, we can operate on it.
Because the program must exist at the beginning and the system allocates the memory, it does not allow the constructor to allocate the memory space, so it is irrelevant to the object, so it can save the memory,
Improve Efficiency
2. static data members are declared only in the class and must be defined and initialized outside the class. The program will not initialize static data members by default.
3. access method:
Class type name: static data member name (this method is recommended)
Class Object Name. static data member name

Static member functions have the following features: (many of the benefits of static member functions are based on the instance of the class, only related to the class, without the this pointer)
1. static member functions are similar to static data members. They are also irrelevant to objects and only related to classes.
2. The static member function does not have the this pointer. The static member function is generally used to access and operate static data members.
3. The static member function can be used during the initialization of the constructor. It is used to pass some information to non-static members for initialization.
4. The static member function does not have the this pointer, so it is almost equivalent to the nonmember function, and an unexpected result is generated.
Benefits: As a callback function, we can combine C ++ and C-based X Window Systems and successfully apply them to thread functions,
The callback functions of some system APIs can be encapsulated into the class in the form of static functions.
5. access method:
Class type name: static data member function name (this method is recommended)
Class Object Name. static data member function name
6. Singleton Mode Policy:
1. Declare the constructor as private and prohibit the user from constructing the object by themselves. Defines a static member function, defines a static object within the function, and returns a reference or pointer to the object.
2. To ensure that the operation of copying and assigning values is not allowed, it is enough to declare the declaration of the two functions as private.
7. In short, from the perspective of OOA/OOD, all functions with definite behavior patterns can be designed to be static without instantiation.
The above are just some basic considerations and are not absolute. There is only one absolute thing: "static functions can be called without instantiation, and they cannot be called or manipulate non-static members ".
Remember this, it is naturally easier to decide when to use and when to use it in actual programming.

 

Non-static member functions can access static member functions. The reason is that a non-static member function has the this pointer and can access any member.
Static member functions cannot access non-static member functions. The reason is: the static member function does not have the this pointer.

If a static data member is defined in the base class, all objects of the base class and all objects of the derived class share the data member.
If the derived class object does not want to share the static members of the base class with the base class object, the derived class needs to block the static members of the base class.
The method is to redefine the member in the derived class to realize that the object of the derived class does not share the static data member of the base class with the base class object, because our compiler adopts a wonderful method: name-mangling
Used to generate a unique flag. However, if you need to use static member data of the base class when shielding the sharing function, use the scope domain operator to access the member of the base class.

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.