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 class
static data_type variable;
#Initializing a static variable within a class
data_type class_name::variable=value;
#Accessing the value of a static variable within a class
Class_name.variable_name;

Example 1: Static variable within a member function

示例1: 成员函数中的静态变量


#include <iostream>
#include <string>
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 <iostream>
#include <string>
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:"<<V.var;cout<<'\n';cout<<"Local Variable:"<<V.x;
}

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:10
Local 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 <iostream>
#include <string>
using namespace std; class ABC
{public:static void stat_func(int a, int b){static int res = a+b;cout<<"Addition:"<<res;}
};int main()
{ABC::stat_func(10,30);
}

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)

  • Static members in C++ — Official DocumentationC ++中的静态成员—官方文档

翻译自: https://www.journaldev.com/38334/static-keyword-in-c-plus-plus

c语言中关键字static

c语言中关键字static_了解C ++中Static关键字的变体相关推荐

  1. java中public static_对java中public、static的理解

    首先是public 在说明这四个关键字之前,我想就class之间的关系做一个简单的定义,对于继承自己的class,base class可以认为他们都是自己的子女,而对于和自己一个目录下的classes ...

  2. 【让你从0到1学会C语言】指针/数组传参以及static关键字

    作者:喜欢猫咪的的程序员 专栏:<C语言> 喜欢的话:世间因为少年的挺身而出,而更加瑰丽.                                  --<人民日报> ...

  3. c++语言static作用,详解c++中的 static 关键字及作用

    注:若没有特指是 静态成员时,默认都是普通成员: 1 类中的普通成员 类中的成员变量 和 成员函数 是分开存储的.其中, 1)每个对象都有独立的成员变量:成员变量可以存储在 栈空间.堆空间.全局数据区 ...

  4. C/C++中static关键字详解

    其实只有一句话需要注意: static在类外初始化,然后才可以在类内修改,其生命周期就是整个程序的生命周期.  静态变量作用范围在一个文件内,程序开始时分配空间,结束时释放空间,默认初始化为0,使用时 ...

  5. static关键字是什么意思?java中是否可以覆盖一个private方法或者static方法

    一.static关键字是什么意思? static关键字表示静态的意思,用于修饰成员变量和成员函数.表示可以在没有类的实例的情况下,用类名.变量名或者类名.函数名,进行访问 二.java中是否可以覆盖( ...

  6. php中如何声明静态属性,PHP使用static关键字声明静态属性和静态方法

    PHP使用static关键字声明静态属性和静态方法 在PHP中,通过static关键字修饰的成员属性和成员方法被称为静态属性和静态方法. 静态属性和静态方法不需要在被类实例化的情况下就可以直接使用. ...

  7. java中static关键字的作用_面试|static 关键字有什么作用

    今天主要学习下Java语言中的static关键字. static关键字的含义及使用场景 static是Java50个关键字之一.static关键字可以用来修饰代码块表示静态代码块,修饰成员变量表示全局 ...

  8. java static关键字的作用是什么_java中的static关键字

    一.static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个"伪全局"的概念,在Java中static表示"全局"或者 ...

  9. C语言之static关键字详解

    目录 前言: 一.几个概念 1.变量 2.局部变量和全局变量 3.变量的作用域 4.变量的生命周期 二.static关键字的作用 三.static关键字修饰局部变量 四.static关键字修饰全局变量 ...

最新文章

  1. 社会科技奖不是新鲜事?如何真正做大
  2. 小白学数据分析-----付费渗透率再研究
  3. POJ1324贪吃蛇(状态压缩广搜)
  4. (解题思路)Entity Framework 如动态创建表或者列
  5. xftp传输文件报错_Xshell+Xftp--Windows平台远程终端解决方案
  6. Oracle 数据库Patch概念性小常识 PSU,CPU,bundle patch
  7. Unit Three-Program test
  8. Linux--文件结构体struct file
  9. c语言中vector函数大全,vector :: empty()函数,以及C ++ STL中的示例
  10. c语言除法加括号么,算不了除法,为什么?
  11. Freebsd上使用pf防火墙
  12. python编程入门电子书-Python编程从入门到实践PDF电子书
  13. Ubuntu使用记录:安装deb软件方法以及apt、apt-get和dpkg的区别
  14. OFFICE技术讲座:影响文字效果的四个因素是哪些
  15. atitit 信息化2.0概论 艾提拉解读版读书笔记.docx 目录 1. 企业信息化1.0时代 1.1.6 C/S模式和B/S模式的好坏 1.1.7 大集中的争议 1.1.8 没有解决的孤岛问
  16. 4. 嵌入式OpenWRT入门基础篇-----设置OpenWRT系统为AP、中继模式
  17. URI、URL、URN介绍
  18. [zt]再谈QQ自动登陆器:提供C#源码下载(下)
  19. 程序人生 - 给IT新人的15点建议:苦逼程序员的辛酸反省与总结
  20. alert的确定和取消

热门文章

  1. iOS常用第三方类库
  2. webservice系统学习笔记8-简单的权限校验
  3. 数据库设计经验浅谈(3,4,5)转载
  4. [转载] numpy.take()从数组中取指定的行或列
  5. 如何理解FPGA的配置状态字寄存器Status Register
  6. httpd配置ResponseHeader
  7. 02: python3使用email和smtplib库发送邮件
  8. ie8兼容background-size属性
  9. 关于 java jdk 环境变量的配置
  10. Iframe的高级操作