Static usage Summary

Source: Internet
Author: User

The static function of C ++ has two usage methods: static in process-oriented programming and static in object-oriented programming. The former applies to common variables and functions, and does not involve classes. The latter mainly describes the role of static in classes.

I. Static in process-oriented design
1. Static global variables
2. Static local variables 3. Static functions 2. object-oriented static keywords (static keywords in the class)

1. static data member

2. static member functions
  I. Static in process-oriented design 1. Static global variables
See the Code:
Static global variables: 1) Global data ZoneAllocate memory; (See http://www.cnblogs.com/menghuizuotian/p/3769891.html for details) 2) uninitialized static global variables will be automatically initialized to 0 by the program (the value of the automatic variable is random unless it is explicitly initialized ); 3) Static global variables are visible in the entire declared file, but invisible outside the file; This prevents conflicts..   2. Static local variables
Before a local variable, add the keyword static to define the variable as a static local variable.
Let's take a look at the Code:
Generally, a variable is defined in the function body, and stack memory is allocated to the local variable whenever the program runs the statement. However, as the program exits from the function body, the system will reclaim the stack memory and the local variables will also become invalid.
But sometimes we need to save the variable value between two calls. The general idea is to define a global variable for implementation. In this way, the variable no longer belongs to the function itself, and is no longer only controlled by the function, causing inconvenience to program maintenance.
Static local variables can solve this problem. Static local variables are stored in the global data zone, instead of in the stack. Each value is kept to the next call until the next value is assigned.
Static local variables have the following features:
(1) The variable allocates memory in the global data zone;
(2) Static local variables are initialized for the first time when the program executes the declaration of the object, that is, subsequent function calls are not initialized;
(3) Static local variables are generally initialized at the Declaration. If no Explicit initialization is performed, the program will automatically initialize them to 0;
(4) It always resides in the global data zone until the program running ends. However, its scope is local scope. When the function or statement block that defines it ends, its scope ends;
 

3. Static Functions
Add the static keyword before the return type of the function. The function is defined as a static function. A static function is different from a common function. It can only be seen in the file where it is declared and cannot be used by other files.

  2. Object-oriented static keywords (static keywords in the class)
1. static data member
In ClassThe keyword static is added before the declaration of the data member. The data member is the static data member in the class. Here is an example of a static data member.
 Static data members have the following features:
1) For non-static data members, each class object has its own copy. Static data members are treated as class members. No matter how many objects are defined for this class, static data members also have only one copy in the program, which is shared by all objects of this type. That is to say, static data members are shared by all objects in the class. For multiple objects in this class, static data members only allocate memory once for all objects to share. Therefore, the values of static data members are the same for each object, and their values can be updated. 2) static data members are stored in the global data zone. Space must be allocated when defining static data members, so they cannot be defined in the class declaration. In Example 5, the statement int myclass: Sum = 0; defines static data members. 3) Because static data Members allocate memory in the global data area, all objects in this class are shared, therefore, it does not belong to a specific class object. Its scope is visible when no class object is generated, that is, when no class instance is generated, we can operate on it. 4) the static data member does not enter the global namespace of the program, so there is no possibility of conflict with other global names in the program;   2. static member functions
It serves all the services of a class rather than the specific objects of a class. Static member functions, like static data members, are internal implementation of the class and are part of the class definition. A common member function generally implies a this pointer, which points to the object itself of the class, because a common member function always belongs to a specific object of a class. Generally, this is the default value. For example, the function FN () is actually this-> FN (). However, compared with common functions, static member functions do not have the this pointer because they are not associated with any objects. In this sense, it cannot access non-static data members of class objects or non-static member functions. It can only call other static member functions.

Static member functions can be summarized as follows:

 

 

  • The keyword static cannot be specified for function definitions that appear in external classes;
  • Static members can access each other, including static member functions accessing static data members and accessing static member functions;
  • Non-static member functions can access static member functions and static data members at will;
  • Static member functions cannot access non-static member functions and non-static data members;
  • Without the additional overhead of this pointer, static member functions increase slightly compared with global functions of the class;
  • Call static member functions. You can use the member access operator (.) and (->) call a static member function for a class object or pointer to a class object. You can also directly use the following format:
    <Class name >:: <static member function name> (<parameter table>)
    Call static member functions of a class.
     
 
Reference: http://blog.csdn.net/hackbuteer1/article/details/7487694#comments



From Weizhi note (wiz)



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.