本文共 4829 字,大约阅读时间需要 16 分钟。
c语言中关键字static
Hello, folks! Today we will be unveiling another important aspect in the series of our C++ tutorials: Variants of static keyword in C++.
大家好! 今天,我们将在我们的C ++教程系列中揭示另一个重要方面:C ++ 中static关键字的变体 。
The static keyword
in C++ makes any variable, object or function a constant one. That means, only one copy of that particular variable or function would be created and will be accessed by all the instances of the class.
C ++中的static keyword
使任何变量,对象或函数成为常数。 这意味着将仅创建该特定变量或函数的一个副本,并且该类的所有实例都将对其进行访问。
Thus, it would help the programmers assist and manage the memory in a better manner because only a single copy of the static variables/functions reside every time an object calls for it.
因此,这将有助于程序员以更好的方式辅助和管理内存,因为每次对象调用静态变量/函数时,它们仅驻留一个副本。
If any data member or variable in C++ is declared as static, it acts like a class variable. Moreover, only one copy of the variable is available for the class objects to access.
如果C ++中的任何数据成员或变量都声明为静态,则其作用类似于类变量。 而且,只有变量的一个副本可供类对象访问。
The memory for the static variables get allocated only once, and further all the function which calls to the variable will available by the single copy of the variable. So, no instance copy of static variable is created.
静态变量的内存仅分配一次,并且所有调用该变量的函数都将由该变量的单个副本提供。 因此,不会创建静态变量的实例副本。
Syntax: Static variable in a member function
语法: 成员函数中的静态变量
static data_type variable = value;
Syntax: Static variable within a Class
语法:类中的静态变量
#Declaration of a static variable within a classstatic data_type variable;#Initializing a static variable within a classdata_type class_name::variable=value;#Accessing the value of a static variable within a classClass_name.variable_name;
Example 1: Static variable within a member function
示例1: 成员函数中的静态变量
#include#include using namespace std; void count_static() { static int stat_var = 1; cout << stat_var <<'\t'; stat_var=stat_var*2;} void count_local() { int loc_var = 1; cout << loc_var <<'\t'; loc_var=loc_var*2;} int main() { cout<<"Manipulation on static variable:\n"; for (int x=0; x<2; x++) count_static(); cout<<'\n'; cout<<"Manipulation on local variable:\n"; for (int y=0; y<2; y++) count_local(); return 0; }
In the above example, we have done manipulations on a static and local variable to understand the difference between their functioning.
在上面的示例中,我们对静态变量和局部变量进行了操作,以了解其功能之间的区别。
When we call the count_static() function, the value is once initialized and creates a single instance of the variable. When the main function encounters the count_static() function, the variable’s value travels through the function.
当我们调用count_static()函数时,该值将被初始化并创建该变量的单个实例。 当主函数遇到count_static()函数时,变量的值将通过该函数。
On the other hand, when we call the count_local() function two times in a loop, the variable will be initialized every time. Thus, the value of local variable remains 1.
另一方面,当我们在循环中两次调用count_local()函数时,该变量将每次都初始化。 因此,局部变量的值仍为1。
Output:
输出:
Manipulation on static variable:1 2 Manipulation on local variable:1 1
Example 2: Static variable in a class
示例2: 类中的静态变量
#include#include using namespace std; class stat_var{ public: static int var; int x; stat_var() { x=0; }};int stat_var::var=10;int main(){ stat_var V; cout << "Static Variable:"<
In the above snippet of code, we have used a static variable within a class. We need to initialize the static variable outside the class, else an exception will be raised.
在上面的代码片段中,我们在类中使用了静态变量。 我们需要在类外部初始化静态变量,否则将引发异常。
Further, we can access the value of the static variable using the class name. Thus, we no need to create an object for the same.
此外,我们可以使用类名访问静态变量的值。 因此,我们无需为其创建对象。
Output:
输出:
Static Variable:10Local Variable:0
Static member functions work with static data values and thus cannot access any non-static variables of the class.
静态成员函数使用静态数据值,因此无法访问该类的任何非静态变量。
Syntax: Static function definition
语法:静态函数定义
static return_type function_name(argument list)
Syntax: Static Function call
语法:静态函数调用
Class_name::function_name(values)
Example:
例:
#include#include using namespace std; class ABC{ public: static void stat_func(int a, int b) { static int res = a+b; cout<<"Addition:"<
Output:
输出:
Addition:40
Thus, in this article, we have understood the working and variants of static keyword in C++.
因此,在本文中,我们了解了C ++中static关键字的工作方式和变体。
翻译自:
c语言中关键字static
转载地址:http://koqzd.baihongyu.com/