简体   繁体   English

在.h 文件中的 class 中声明 static 变量和在 .cpp 文件中声明“全局”变量有什么区别

[英]What is the difference between declaring a static variable in a class in the .h file and a “global” variable in the .cpp file

I don't know if my title is expressed with the right terminology, if it isn't, please correct me so I can update it.我不知道我的标题是否用正确的术语表达,如果不是,请纠正我,以便我更新它。 However this is my question expressed with code examples: What is the difference, when it comes to the keyStates variable, between example A and B?然而,这是我用代码示例表达的问题:当涉及到 keyStates 变量时,示例 A 和 B 之间有什么区别?

Example A (Where the "keyStates" variable is defined as a static variable in the class in the.h file):示例 A(其中“keyStates”变量在 .h 文件的 class 中定义为 static 变量):

// Input.h
class Input
{
public:
    static bool GetKeyDown(KeyCode keycode);
private:
    static std::unordered_map<KeyCode, KeyState> keyStates;
}

// Input.cpp
#include "Input.h"

bool Input::GetKeyPressed(KeyCode keyCode)
{
    for (auto Code : AllKeyCodes)
    {
        if (Code == keyCode)
        {
            return KeyState::PRESSED == keyStates.find(Code)->second;
        }
    }
    return false;
}

Example B (Where the "keyStates" variable is defined without static in the.cpp file):示例 B(其中“keyStates”变量在 .cpp 文件中定义为没有 static):

// Input.h
class Input
{
public:
    static bool GetKeyDown(KeyCode keycode);
}

// Input.cpp
#include "Input.h"

std::unordered_map<KeyCode, KeyState> keyStates;

bool Input::GetKeyPressed(KeyCode keyCode)
{
    for (auto Code : AllKeyCodes)
    {
        if (Code == keyCode)
        {
            return KeyState::PRESSED == keys.find(Code)->second;
        }
    }
    return false;
}

Well, when you have a static member of a class (whether it's a field or a method), it is "global" for the whole class, so that:好吧,当您拥有 class 的 static 成员(无论是字段还是方法)时,它对于整个 class 都是“全局的”,因此:

  1. To access it from anywhere you need to use ClassName::method() or ClassName::field .要从需要使用ClassName::method()ClassName::field的任何地方访问它。
  2. You can share or restrict access to this member with access modifiers: private, public and ptorected.您可以使用访问修饰符共享或限制对该成员的访问:private、public 和 ptorected。
  3. This member belongs to the class, not to any specific objects.该成员属于 class,不属于任何特定对象。 You cannot use this from such methods The full list of restrictions is there: https://en.cppreference.com/w/cpp/language/static您不能从此类方法中使用this限制的完整列表在那里: https://en.cppreference.com/w/cpp/language/static

The static global variable, on the other hand, is like a usual global variable, except it "exists" only for the current compilation unit.另一方面,static 全局变量就像一个普通的全局变量,只是它仅对当前编译单元“存在”。 You can not use it from anywhere except this particular.cpp file.除了这个特定的.cpp 文件,您不能在任何地方使用它。 With a usual global variable if there are two compilation units (cpp files) with the same global variable int a , the code won't compile.如果有两个具有相同全局变量int a的编译单元(cpp 文件)使用通常的全局变量,则代码将无法编译。 Here is more on that: C/C++ global vs static global这里有更多内容: C/C++ global vs static global

Also, you can use anonymous namespaces anywhere where you would use static global variables (or methods, or even types).此外,您可以在使用 static 全局变量(或方法,甚至类型)的任何地方使用匿名命名空间。

UPD.: Yet another difference here. UPD.:这里还有另一个区别。 When you put your keyStates into cpp file as a static global (or part of anonymous namespace), the implementation detail is hidden from class definition and the.h file.当您将keyStates作为 static 全局(或匿名命名空间的一部分)放入 cpp 文件时,实现细节从 class 定义和 .h 文件中隐藏。 So you can change it whenever you wish w/o changing the interface and having to recompile anything except the cpp file.因此,您可以随时更改它,而无需更改界面并且必须重新编译除 cpp 文件之外的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在标头而不是源文件(cpp / h)中声明变量之间有什么区别 - What is the difference between declaring a variable in a header instead of the source file (cpp/h) .cpp 文件和 .h 文件有什么区别? - What is the difference between a .cpp file and a .h file? C++,静态局部变量(方法)与全局(文件)变量有什么区别? - C++, What is difference between static local variable(method) vs global(file) variable? 前向声明一个类和声明一个类的变量有什么区别? - What is the difference between forward declaring a class and declaring a variable of class? 全局const初始化以及.h或.cpp文件中的构造函数之间的差异 - Global const initializing and difference between constructor in .h or .cpp file 可以在.h文件中声明静态全局变量吗? - Okay to declare static global variable in .h file? 在.h文件或.cpp文件中实现类之间的区别 - Difference between implementing a class inside a .h file or in a .cpp file .h(头文件)和.cpp文件有什么区别? - What is the difference between a .h(header file) and a .cpp file? 类中的静态成员变量和 C++ 中的全局变量有什么区别? - what is the difference between static memeber variable in a class and global variable in C++? cpp文件中的全局变量 - Global variable inside cpp file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM