C/C++-static keyword


Preface

That's how it is. One day, a friend asked me a few questions about the static keyword. Since I have just started C++ (after learning the C language), I feel that I can’t answer my friend’s questions systematically. So imagine: If you have an interview The official asked questions about static keywords, how should I answer them, and the answers were very organized. For this purpose, I will make a preliminary summary under this blog.

1. What are the uses of Static keywords?

The initial summary of the role of the static keyword is as follows:

  1. In the body of the function, a variable declared as static is in the body of the function, and its value is unchanged.
  2. In a module but not in the function body, a variable declared as static can be called by any function in the module, but cannot be called by a function outside the module. Its essence is a global variable in this module.
  3. In a module, a function declared as static can only be called by other functions in the module. In layman's terms, this function is restricted to the module where it is declared.

Let's talk about it in detail below:

static modifies global variables

We all know that global variables are stored in the data area of ​​the program space. After adding static, their scope has changed slightly. Variables modified by static can only be used inside the defined file. Then when you have multiple files, other files cannot access the static global variables you define.

static modifies local variables

After static modifies a local variable, the variable is saved in the data area, and the variable becomes what we call a static variable. The life cycle of the variable stored in the data area is the same as the program, that is, it is initialized between the main function and at the end of the program. destroy.

static modified function

After the static function is modified, the static function can only be called in this file. For the static function, its declaration and definition need to be placed in the same file.


2. When static is object-oriented

Static data member

In C++, if a certain attribute is shared by the entire class and does not belong to any specific object, we use the static keyword to declare static members.

What are the characteristics of static members after adding the static keyword?

  1. Static data members are stored in the global data area, allocated to storage space when they are defined, and are not declared in the class.
  2. A static data member is a member of a class. Even if you define 10,000 class objects, there is only one copy of the static data member (so it seems to save space) and it is visible to all objects of the class. To put it bluntly, 10,000 objects in a class can operate on static data members, while for non-static members, each object has a copy.
  3. The static data member does not belong to any object. When there is no instance of the class, its scope is visible. When there is no object, you can also operate on the static data member.
  4. Static data members and ordinary data members follow the same access rules as public\protected\private
  5. Initialization format of static data members: <data type><class name>::<static data member name>=<value>
  6. There are two ways to access static data members of a class: <class object name>.<static data member name> or <class type name>::<static data member name>

Let's compare global variables and static data members:

  1. Static data variables can be private members, but global variables cannot.
  2. Static data members do not enter the global namespace of the program, so there is no possibility of conflict with other global names in the program.

Static member function

Static member functions are actually similar to static data members. Static member functions belong to the entire class, not to a specific object.

Features of static member functions:

  1. The static member function does not have the this pointer, which also determines that it cannot access the non-static data members of the tear object, nor can it access the non-static member functions, and can only call the static member functions of the class.
  2. The static keyword cannot be added to functions that appear outside the class.
  3. Non-static member functions can arbitrarily access static member functions and static data members.

———————————————————————————

After the above study, there are some problems as follows:

Q1: What are the usage scenarios of static member variables and static member functions?

Answer: To put it plainly, static member functions exist to deal with static member variables.

Q2: Is there a this pointer for static member functions?

Answer: When the program calls a non-static member function, the compiler will assign the starting address of the object to the this pointer of the member function. But for a static member function, it does not belong to a certain object, it is common to all objects, so it does not have this pointer.

Q3: How is the memory allocated?

Answer: The static member variable belongs to the class. It is stored in the data area and does not belong to a specific object. The memory of the object is opened up on the heap. Even if multiple objects are created, the memory is only allocated once for the variable. , All objects use the data in this memory.

Three, summary

The above are some of the knowledge points that can be understood at present. If there is new knowledge in the later period, it will be updated continuously. If there are any errors in this article, I hope the big guys will point them out, and I will humbly correct them.

Guess you like

Origin blog.csdn.net/xhuyang111/article/details/115176130