Home >Backend Development >C++ >What are the application scenarios of the static keyword in C++ functions?

What are the application scenarios of the static keyword in C++ functions?

王林
王林Original
2024-04-11 21:51:02824browse

The static keyword in C can be applied to functions to achieve the following scenarios: define private or protected class methods to achieve intra-class or derived class access; create global functions so that the functions can be accessed anywhere in the program; create thread safety function to ensure safe use in concurrent environments.

C++ 函数static关键字的应用场景有哪些?

Application scenarios of C function static keyword

static The keyword is widely used in C In function declaration, it controls function scope and lifetime. Some main application scenarios are listed below:

1. Define private or protected class methods

static Functions can be declared private or protected, which means they can only be accessed within the class or in derived classes. This is useful for creating utility functions that are used only for internal administration.

2. Create global functions

#static Functions can be defined with global scope, which means they can be accessed from anywhere in the program. This is useful for creating library functions or utility functions.

3. Create thread-safe functions

static Functions are thread-safe, which means they can be used safely in a concurrent environment. This is because they only exist as a single copy and are not modified by concurrent access.

Practical case:

Private static function:

class MyClass {
public:
    void foo() {
        static int count = 0;  // 私有静态变量
        count++;
        std::cout << "Call count: " << count << std::endl;
    }
};

int main() {
    MyClass obj;
    obj.foo();  // 输出:Call count: 1
    obj.foo();  // 输出:Call count: 2
    // count 变量只在 foo() 方法中可见,不能从主函数中访问。
}

Protected static function:

class Base {
protected:
    static int value;  // 受保护静态变量
};

class Derived : public Base {
public:
    static void set_value(int v) {
        Base::value = v;  // 可以访问基类的受保护静态成员
    }

    static int get_value() {
        return Base::value;  // 可以访问基类的受保护静态成员
    }
};

int main() {
    Derived::set_value(100);
    std::cout << "Value: " << Derived::get_value() << std::endl;  // 输出:Value: 100
    // 只能通过 Derived 类访问 value 变量,因为它继承了 Base 类。
}

Global static function:

static int global_count = 0;  // 全局静态变量

int increment_count() {
    return ++global_count;  // 返回并递增全局计数器
}

int main() {
    std::cout << increment_count() << std::endl;  // 输出:1
    std::cout << increment_count() << std::endl;  // 输出:2
    // 全局计数器在程序的整个生命周期中都可以访问。
}

Thread-safe static function:

class ThreadSafeClass {
public:
    static int get_random_number() {
        static std::random_device rd;  // 静态随机数生成器
        static std::mt19937 gen(rd());
        return gen();
    }
};

int main() {
    std::thread t1([] {
        for (int i = 0; i < 10000; i++) {
            ThreadSafeClass::get_random_number();
        }
    });

    std::thread t2([] {
        for (int i = 0; i < 10000; i++) {
            ThreadSafeClass::get_random_number();
        }
    });

    t1.join();
    t2.join();
    // 即使从多个线程并发访问,get_random_number 函数也能生成线程安全的随机数。
}

The above is the detailed content of What are the application scenarios of the static keyword in C++ functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn