In-depth understanding of the static (Static) keyword in C++ and its application scenarios

introduction:

In C++, the static keyword is a common and important concept used to describe variables, functions and class members. It has unique characteristics and scope, and can play an important role in different application scenarios. This article will discuss static keywords in C++ in detail, including static variables, static functions and static class members, and analyze the differences between them and global variables. At the same time, we will provide code examples to help readers better understand and apply this concept.

Static Variables¶

A static variable is a variable declared inside a function or in a class and is decorated with the static keyword. They have the following characteristics:

There is only one copy of static variables in memory. No matter how many objects are created or how many times functions are called, the memory space of static variables is shared.
Static variables exist all the time while the program is running, and will not be destroyed until the end of the program.
The scope of a static variable is limited to the function or class in which it is declared, but it can maintain state between different function calls.
Application scenarios of static variables:

In a function, it can be used to record the number of function calls, such as a counter.
Within a class, it can be used to share data between class members, or as a marker for global state.
Sample code:

void increment() {
    
    
    static int count = 0;
    count++;
    std::cout << "Count: " << count << std::endl;
}

int main() {
    
    
    increment();  // 输出:Count: 1
    increment();  // 输出:Count: 2
    increment();  // 输出:Count: 3
    return 0;
}

Static Functions¶

A static function is a function declared in a class and decorated with the static keyword. They have the following characteristics:

Static functions do not depend on the instance of the class and can be called directly through the class name without creating an object.
Static functions cannot access non-static members because non-static members are associated with instances of the class.
Application scenarios of static functions:

Define some general helper functions in the class, not dependent on the instance of the class.
Sample code:

class MathUtils {
    
    
public:
    static int add(int a, int b) {
    
    
        return a + b;
    }
};

int main() {
    
    
    int result = MathUtils::add(2, 3);
    std::cout << "Result: " << result << std::endl;  // 输出:Result: 5
}}

Static Class Members

Static class members are static variables or static functions declared in a class. They have the following characteristics:

Static class members have only one copy in memory, no matter how many instances of the class are created, the memory space of static class members is shared.
Static class members do not depend on the instance of the class and can be accessed through the class name.
Application scenarios of static class members:

Shared data within a class, used to track state between instances of the class.
Define some general helper functions in the class, not dependent on the instance of the class.
Sample code:

class Circle {
    
    
private:
    static const double PI;
    static int count;

public:
    static int getCount() {
    
    
        return count;
    }
};

const double Circle::PI = 3.14159;
int Circle::count = 0;

int main() {
    
    
    Circle c1;
    Circle c2;
    Circle c3;
    std::cout << "Count: " << Circle::getCount() << std::endl;  // 输出:Count: 3
    return 0;
}

The difference between static variables and global variables

Scope: The scope of a static variable is limited to the function or class in which it is declared, while the scope of a global variable is the entire program.
Life cycle: Static variables always exist during the running of the program and will not be destroyed until the end of the program, while global variables also have the same life cycle.
Visibility: Global variables can be accessed by functions in other files (if declared extern), while static variables are restricted to the function or class that declares it.

in conclusion:

Static keywords play an important role in C++ and are used to describe static variables, static functions and static class members. Static variables and static functions are used inside functions and within classes to maintain state between different function calls or class instances. Static class members are used to share data within a class or to define common helper functions. The difference between a static keyword and a global variable is scope and visibility. The scope of a static variable is limited to the function or class in which it is declared, while the scope of a global variable is the entire program. By understanding the characteristics and application scenarios of static keywords, we can better design and organize C++ programs, and achieve flexible and efficient code structures.

Guess you like

Origin blog.csdn.net/qq_46017342/article/details/131159404