I was helping a student with one of their projects and came upon something I couldn't quite figure out.
They were provided with a single source file with some code provided, having to fill in the missing functionality and then answer some questions in the comments at the bottom.
Already present were static instances of classes that had to be defined by the student like so:
// onlyfile.cpp
// Student entered
class A { A(std::string)... };
class B : public class A { B(std::string)...};
class C : public class B { C(std::string)...};
// Provided by professor
static A instanceA1("One");
static A instanceA2("Two");
static B instanceB1("One");
static B instanceB2("Two");
static C instanceC1("One");
//...
Then one of the questions at the bottom asked:
Why did [name] specify the globals as static? I.e., what does the static keyword do?
As far as I'm aware, unlike the use of the static keyword in all other contexts (i.e. static member variable/function, static variable in function, etc.) which changes behavior and data storage, declaring a global variable as "static" only reduces it to "file scope", essentially hiding it from the linker and nothing more.
I believe this to be an oversight by the professor who perhaps originally intended to have the code setup differently, or a trick question, as since there was only a single source file an no headers used to compile this program the use of static here effectively changes nothing. There is no other file for a possible name clash or encapsulation consideration, other than the std includes which have everything under the std namespace anyway.
Perhaps it does change the location the instance is stored at in memory (like it potentially does in its other uses), though I don't think so and it wouldn't be a relevant lesson for this assignment based on its instructions/goals sheet.
As a sanity check I confirmed that the program compiles, runs, and produces identical output with and without the static qualifier on those variables.
Does the use of static here due something else that I am unaware of?