Keyword Static Summary <First in 9CBS, please give some points (wrong and deficiencies)>

zhaozj2021-02-16  49

Static Keyword If it is not introduced into the C language, it violates the "no for no compatibility rules" in the low-level language design support rules in C design. The reason is very simple. There is STATIC in C language to play it well. The role, so C is also introduced to static should be a matter, while actual C practice is not only introducing Static, but also expands it in object-oriented programming (the concept of static data member and static function members), this makes The concept of Static has been extended. For new static member variables (objects) and static function members, they should focus more more, the following from Static's role, classification in C , and some of the more typical applications. Make a summary

One: Static (static) role

First of all, a significant role is to solve the problem of all local names space. Everyone knows that global variables are modified in the definition, which is of course its advantage, but it has indeed solved a lot of problems, but this pair Object-oriented ideas are compatible, it is destroyed good encapsulation, polluting the namespace of the program, and for these shortcomings, it is the problem that Static can solve, we can define static objects in functions, structure, and classes. When the variable is defined as STICC, the compiler will store the variable in the stack space of the program's static memory area (data segment) domain instead of the document.

Second, Static will limit the visibility of its modified variable objects in this compilation unit (that is, the file with file scope), making it an internal connection. This is added to "Extern" with a normal global variable to be used in multiple files.

2: Static classification

At present, there are five static objects in C languages, respectively, static local variables (objects), static global variables (objects), static functions (static global functions), static member variables (objects), static member functions.

Static local variables, static global variables, and static functions are compatible with C language, static member variables are concepts corresponding to C introduction, and static member functions are the premise of maintaining classes. The concept of a better way to make a better way to make a static member variable, so the static member function is currently in the latest one of the latest a static object (including C all static variables and static member functions).

1. Static partial variable (object)

Static local variable (object) Pass also refers to a static variable (object) defined in the function. When the function is first called, the program will allocate storage space for its data segment, and initially, when the function When the call is complete, this variable (object) saves the status after the function is called, but will not be used by other functions. When the function is called again, it is not allocated to allocate space and initialization, but directly use it. The result of the secondary call. Take a look at the simple program below:

#include

#include

Using namespace std;

Void foo (BOOL i)

{

Static int A = 0;

INT C = 0;

IF (i) {

Static int b = 0; b;

COUT << "b =" << b << endl;

}

a;

C;

COUT << "a =" << a << endl;

COUT << "c =" << c << endl;

}

int main ()

{

Foo (false); // variable, A, C initialization and 1, so A = 1, c = 1, this is B and did not initialize

// and allocated space, the function calls complete C is reclaimed, and A is a static variable,

// So did not recycle.

COUT << "-----------------" e e;

Foo (TRUE); // Variable A plus 1 on the last call, so A = 2, C re-initial allocation space and add 1,

// Therefore, C = 1, the variable B is initialized and allocated after the space is added, so B = 1.

COUT << "-----------------" e e;

Foo (false); // Variable A plus 1 on the last call, so A = 3, C re-initial allocation space and add 1,

// So c = 1, the variable B protects the state, so B = 1.

COUT << "-----------------" e e;

FOO (TRUE); // Variable A plus 1 on the previously called, so A = 4, C re-initial allocation space and add 1,

// So C = 1, the variable B plus 1 on the last call, so B = 2.

System ("pause");

Return 0;

}

//

From the running knot from the program, it can be seen that the local static variable is only the allocation and configuration of the intake space, and the function call is not recycled immediately, and the procedure runs the end. Because it only has a shared space address (Object Multithreading), so it needs to be locked in the multi-line program program.

2. Static global variable (object)

Static global variables (objects) refer to the basis of global variables, plus Static modifiers, which have two features of file scope and static survival. Specifically, it means that the variables that are invisible in other source files are only available in the source file defined (file scope). It is the difference between the full variables: the full variable can be explained as an external variable (Extern), which means that the file scope is used by other source files, and the static full variable can not be explained as an external, that is, it can only Use by the source file.

3. Static function (static global function)

The static function is generally relatively small to use the normal function if static modification, has a cross file scope, if static modification, similar to the static global variable, has a file scope, the general function itself is a common purpose (Whether it is a function definition file or a file as an Include object), if you add Static Lord before the function, the function can only be used in this document, other files cannot be used, we do not want to do this, so, in general In the code, few functional static functions (static global functions) do not exist.

4. Static member variables (objects)

When such a static variable (object) in the program, the object will only be assigned a shared storage order for this class, and the static member variable is a class range, not a specific object, quite The global variable (object) within the class range (object), of course, it can also be said to be a self-type object, and its value can be updated. As long as the value changes the static member change once, all object accessors will be the same value after the update (multi-thread should be controlled by themselves), which can improve time efficiency. Static member variables (objects) thveravulate the class as namespace, in order to better implement information, you can set it to private, and then through the function (will be speech in the back application), the program is a static data member The access will be converted to the unique Extern entity of the class (as for how to convert, different compilers will have different algorithms, please refer to "Inside C Object Model"), because of this, so The inside of the class can directly define the STIC member of its own type, without causing the unlimited recursive recursive, not the static self type, can only be a pointer or reference, in fact, this is the end of the class definition is The semicolon after braces is related, which will not be determined to determine the size of the class, and the size of the own type of pointer or the quote is determined (the space of a pointer). Such as follows:

Class a {

// ......

Private:

// ......

Static A1; // OK, because A1 is a static member object.

A A2; // Error cannot determine the size of the A type at this time.

A * a3; // / / / / OK can determine the size of the A * type, a size of a pointer to the A type

A & A4; // / / / OK can determine the size of the A & Type, a size of a pointer to the Type A

}

Like global variables, static member variables can only provide one sense, and class declarations only declare a class of "size and specifications", do not perform actual memory allocation, so its initialization will not be placed in header files Instead, it should be put into the implementation file of the class member function, and the external definition of the category declared in the header file, because it will be repeatedly defined in multiple source files that use this class. The static ordered type of the const type can be initialized directly, but its definition should be outside the class definition:

//head File

Class Print {

Private:

Static const Int count = 10; // Direct initialization

String info [count];

}

Const int print :: count; // You can also implement files in the header file, because it is not a common allocation space

5. Static member function

The introduction of static member functions is actually in order to better access static member variables (objects), and static member variables (objects) are not limited to a specific object, and ordinary member functions are not serving specific objects, Then use a normal member function to make a static member variable (object) access, it will cause a static member variable to constrain to a specific object (the reason is because the ordinary member function is constrained by the specific object), using a static member function Will solve these problems very well. Therefore, the static member function has a static member variable (object) similar, regardless of the form of a static member function of the specific object, a pointer or reference, :: operator, static member function pointer) Will be converted into a call for non-member functions (normal functions). The maximum feature of static member functions is that there is no THIS pointer to lead to some of the features:

1. You cannot directly access non-state members in the class, you can only access static member variables (objects) directly.

From the language level, the ordinary member function can access non-static members on the THIS pointer, and the static member function does not have the THIS pointer, so it cannot be implemented.

2. Can't be const, volatile or virtual.

Static member functions This direction cannot access non-static members, so Const does not make sense to it. Virtual illustrates that different classes may not have multiple different implementations in a class inheritance system, and the static member function is only one, so static is not suitable for Virtual (personal understanding, only for reference, may also be an error code 噢 j )

3. Do not need to be bind to class objects to be called.

The static member function itself is to solve this problem. J.

4. Can be used as a callback function (Callback).

The static member function is the constraint of the THIS pointer to the pointer to a normal function.

转载请注明原文地址:https://www.9cbs.com/read-25989.html

New Post(0)