Functions of static member variables and static member functions in C ++ class (1)

Source: Internet
Author: User

References: http://blog.chinaunix.net/uid-14114479-id-3035143.html

Http://www.cnblogs.com/lzjsky/archive/2011/01/24/1943199.html

     

Data members can be divided into static variables and non-static variables. static member: A member in a static class is added with a static modifier, which is a static member. you can directly use the class name + static member name to access this static member. Because the static member exists in the memory, non-static members need to be instantiated to allocate the memory, so static members cannot access non-static members .. because static members exist in the memory, non-static members can directly access static members in the class.
Non-static member: all non-static members are non-static members. After the class is instantiated, it can be accessed by the instantiated class name .. the lifetime of a non-static member is determined by the lifetime of the class .. static members do not have the concept of survival, because static members always reside in the content ..
A class can also contain static and non-static members. The class also includes static constructors and non-static constructors .. it is summarized in two aspects: the first is relative to the process-oriented, that is, the class is not involved in this aspect, and the second is relative to the object-oriented, it mainly describes the role of static in the class. --------------------------------------------------------- I am the delimiter of the split line and the static keyword in the process-oriented design.

1. Static global variables

Definition: Before a global variable, add the keyword "static". The variable is defined as a static global variable. Features: A. The variable allocates memory in the global data zone. B. Initialization: If Explicit initialization is not performed, the value 0 is implicitly initialized (automatic variables are random unless explicitly initialized ). C. The access variable is only visible in the source file. Strictly speaking, it should be defined from the beginning to the end of this file. Example (from the C ++ Programming Tutorial --- Qian Neng, editor of p103) // file1.cppview code

// Example 1 # include <iostream. h> void FN (); static int N; // defines the static global Variable Void main () {n = 20; cout <n <Endl; FN ();} void FN () {n ++; cout <n <Endl ;}

D. The constant declared in the file scope is static storage by default.

All static variables are allocated memory in the global data zone, including the static local variables to be mentioned later. For a complete program, the distribution in the memory is as follows:

            

In the code area, global data area, stack area, the dynamic data generated by new programs is stored in the stack area, and the automatic variables in the function are stored in the stack area. Automatic variables usually release space as the function exits, and static data (even static local variables in the function) is stored in the global data zone. The data in the global data zone does not release space because the function exits. Careful readers may find that the code in Example 1 changes the static int N; // defines the static global variable to: int N; // defines the global variable program to run normally. Indeed, defining global variables can share variables in files, but defining static global variables has the following benefits: static global variables cannot be used by other files; (it seems to be different from extern) variables with the same name can be defined in other files without conflict. You can change the preceding sample code to the following view code:

// Example 2 // file1 # include <iostream. h> void FN (); static int N; // defines static global variables (which can only be used in this file) void main () {n = 20; cout <n <Endl; FN () ;}// file2 # include <iostream. h> extern int N; (this variable can be referenced in other files) void FN () {n ++; cout <n <Endl ;}
Compile and run example 2, and you will find that the above Code can be compiled separately, but an error occurs during link. Try to change the static int N; // define the static global variable to int N; // define the global variable and re-compile the program to understand the difference between the global variable and the static global variable.

2. Static local variable definition: When the static keyword is added before the local variable, the static local variable is defined. Here is an example of static local variables: view code

//Example 3#include <iostream.h>void fn();void main(){      fn();      fn();      fn();}void fn(){      static n=10;      cout < <n < <endl;      n++;}

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.

Features: A. The variable allocates memory in the global data zone. B. Initialization: If Explicit initialization is not performed, the value 0 will be implicitly initialized, and subsequent function calls will not be initialized. C. It always resides in the global data zone until the program is running. But its scope is local scope. When the function or statement block defining it ends, its scope ends. 3. Static functions (note the differences between them and static member functions of the class): a function is defined as a static function by adding the static keyword before the return type of the function. Feature: 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. Static function example: view code

// Example 4 # include <iostream. h> static void FN (); // declare the static function void main () {fn ();} void FN () // define the static function {int n = 10; cout <n <Endl ;}

Benefits of defining static functions: static functions cannot be used by other files; functions with the same name can be defined in other files without conflict;

  

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.