Home > Backend Development > C++ > body text

The usage and function of static in c++

下次还敢
Release: 2024-05-08 02:33:19
Original
362 people have browsed it

The static keyword in C is used to modify variables, functions, and class members, instructing the compiler to retain their scope and storage duration. Its usage includes declaring static variables to retain values ​​across function calls or object destruction. Declare static member variables to share data between different instances of a class. Use static functions to provide class-level functionality without creating an instance of the class. Advantages of the static keyword include improved efficiency, enhanced testability, and useful when you need to retain state, share data, or provide class-level functionality.

The usage and function of static in c++

The usage and function of static in C

What is it

static is a keyword in C used to modify variables, functions and class members. It instructs the compiler to retain its scope, storage duration, and linkage properties throughout the lifetime of the program.

Usage

Variables

  • Declaring static variables inside the function can be retained every time the function is called. its value.
  • Declaring static variables inside a class can be shared by all instances in the class.
<code class="cpp">int main() {
  static int x = 10;  // 保留函数调用之间的值
  return 0;
}</code>
Copy after login

Function

  • static function cannot access non-static members and non-static data.
  • static functions can be accessed outside the class without creating an instance of the class.
<code class="cpp">class MyClass {
 public:
  static int add(int a, int b) {
    return a + b;
  }
};

int main() {
  MyClass::add(1, 2);  // 直接调用 static 函数
  return 0;
}</code>
Copy after login

Class members

  • static member variables are shared among all instances of the class.
  • static member functions can only access static member variables and functions.
<code class="cpp">class MyClass {
 public:
  static int count = 0;  // 静态类变量
  static void increment() {
    count++;
  }
};

int main() {
  MyClass::increment();  // 通过类名访问 static 函数
  cout << MyClass::count << endl;  // 访问 static 变量
  return 0;
}</code>
Copy after login

Function

The static keyword is useful in the following scenarios:

  • Retained state: static variables can retain values ​​after function calls or object destruction.
  • Shared data: static member variables can share data between different instances of the class.
  • Provide class-level functionality: static functions can provide class-level functionality without creating an instance of the class.
  • Improve efficiency: static variables and functions can save memory because they are initialized only once while the program is running.
  • Enhanced testability: static functions can be easily tested independently of the class.

The above is the detailed content of The usage and function of static in c++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template