1

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?

Example A (Where the "keyStates" variable is defined as a static variable in the class in the .h file):

// 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):

// 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;
}
  • Now, try to make your class a huge class, with hundreds of methods, such that it's impractical to define all of their methods in a single `.cpp` file, and you have to split it into several. With that in mind, try rereading your question, and see if you can see the difference, by yourself. – Sam Varshavchik Jul 11 '20 at 11:17
  • 1
    The main difference is the visibility of the `keyStates` variable to the rest of the code. – john Jul 11 '20 at 11:17
  • 1
    Static = global within the namespace of the class. – Michael Chourdakis Jul 11 '20 at 11:28
  • 1
    Also, the `private` keyword in your first example plays an important role. – Adrian Mole Jul 11 '20 at 11:46

1 Answers1

1

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:

  1. To access it from anywhere you need to use ClassName::method() or ClassName::field.
  2. You can share or restrict access to this member with access modifiers: private, public and ptorected.
  3. This member belongs to the class, not to any specific objects. You cannot use this from such methods The full list of restrictions is there: 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. You can not use it from anywhere except this particular .cpp file. 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. Here is more on that: 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).

UPD.: Yet another difference here. 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. So you can change it whenever you wish w/o changing the interface and having to recompile anything except the cpp file.

smyatkin_max
  • 365
  • 2
  • 10
  • Thank you for your answer @smyatkin_max, you cleared up a few things. However I wasn't asking about any "static global variable" instead I was asking about the difference between a static member variable and a variable without static in the .cpp file. This I still don't understand. – Anton Eriksson Jul 11 '20 at 15:48