什么是静态变量? [英] What are static variables?

查看:187
本文介绍了什么是静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是静态变量设计? static

What are static variables designed for? What's the difference between static int and int?

推荐答案

static 有四个单独的用途,其中只有两个是密切相关的:

The static keyword has four separate uses, only two of which are closely related:


  • static在全局和命名空间范围(应用于变量和函数)内部联系

    • 这由替换未命名的命名空间,并且与其余的

    • 无关,其他的往往暗示某种唯一性,但内部链接意味着可以有多个具有相同名称的对象,只要每个对象都有内部链接,并且每个转换单元只有一个

    • static at global and namespace scope (applied to both variables and functions) means internal linkage
      • this is replaced by unnamed namespaces and is unrelated to the rest
      • in particular, others tend to imply some sort of uniqueness, but internal linkage means the opposite: you can have many objects with the same name, as long as each has internal linkage and you only have one per translation unit

      • 的所有实例之间是共享的,它更像是它们独立于任何类实例

      • 静态方法


      • 没有这个指针;

      • 静态局部变量(在函数中)会在每个函数调用的范围内持续存在

      • no this pointer; can call without an instance

      静态数据成员和静态局部变量都可以成为隐藏的全局状态,应谨慎使用。

      Both static data members and static local variables can become hidden global state, and should be used carefully.

      现在哪两个密切相关?它不是两个类成员—关于全局状态的警告将它移除。您可以将静态数据成员视为静态局部变量,其中它们所属的函数都是该类的所有方法,而不是单个函数。

      Now which two are closely related? It's not the two class members—the warning about global state gives it away. You can consider static data members as static local variables, where the functions to which they belong are all methods of the class, instead of a single function.

      我发现很多< a href =http://stackoverflow.com/search?q=%5Bc%2B%2B%5D+static>相关问题,但令人惊讶的是,没有重复。

      I found many related questions, but, surprisingly, no duplicates.

      这篇关于什么是静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆