博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言中关键字static_了解C ++中Static关键字的变体
阅读量:2531 次
发布时间:2019-05-11

本文共 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关键字的变体



静态关键字实际上是什么意思? (What does static keyword actually mean?)

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.

因此,这将有助于程序员以更好的方式辅助和管理内存,因为每次对象调用静态变量/函数时,它们仅驻留一个副本。



形式1:静态数据成员 (Variant 1: Static data members)

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


变体2:C ++中的静态函数 (Variant 2: Static function in C++)

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


有关静态成员功能的要点 (Important points regarding static member function)

  • Static member functions cannot be overloaded

    静态成员函数不能重载
  • Any static member function cannot be virtual

    任何静态成员函数都不能是虚拟的
  • Static functions do not operate with this pointer

    静态函数无法与此指针一起使用


结论 (Conclusion)

Thus, in this article, we have understood the working and variants of static keyword in C++.

因此,在本文中,我们了解了C ++中static关键字的工作方式和变体。



参考资料 (References)

翻译自:

c语言中关键字static

转载地址:http://koqzd.baihongyu.com/

你可能感兴趣的文章
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>