[c++] --- Interview knowledge points

1 c++ keywords

1.1 static

static role: control the storage method and visibility of variables.
**Function 1: Modify local variables:** Generally, local variables are stored in the stack area in the program, and the local life cycle ends when the execution of the containing statement block ends. However, if modified with the static keyword, the variable will be stored in the static data area, and its life cycle will continue until the end of the entire program execution. However, it should be noted that although the local variable is modified with static, its life cycle and storage space have changed, but its scope has not changed, and the scope is still limited to its statement block.
**Function 2: Modify all variables:** For a global variable, it can be accessed not only in this file, but also in other source files in the same project (add extern for Just declare). Using static to modify the global variable changes its scope, from the original visibility of the entire project to the visibility of this file.
**Function 3: Modify functions:** Use static to modify functions. The situation is similar to modifying global variables. It also changes the scope of the function.
**Function 4: Modify class:** If a function in a class is modified with static in C++, it means that the function belongs to a class and not to any specific object of this class; if a function in the class is modified If a variable is statically modified, it means that the variable and all objects are owned, and there is only one copy in the storage space, which can be called through classes and objects. (Supplement: Static non-constant data members can only be defined and initialized outside the class, and can only be declared within the class.)
Function 5: Class member/class function declaration static
function body stat

Guess you like

Origin blog.csdn.net/weixin_42445727/article/details/131246835