1

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?

oblivioncth
  • 315
  • 3
  • 11
  • Possible duplicate: https://stackoverflow.com/questions/572547/what-does-static-mean-in-c – Durstann Dec 05 '20 at 02:33
  • Does this answer your question? [The static keyword and its various uses in C++](https://stackoverflow.com/questions/15235526/the-static-keyword-and-its-various-uses-in-c) – Krishna Kanth Yenumula Dec 05 '20 at 02:46
  • @KrishnaKanthYenumula - "If it's in a namespace scope (i.e. outside of functions and classes), then it can't be accessed from any other translation unit. This is known as "internal linkage" or "static storage duration" - This confirms what I said above, that static here simply restricts the usage of these variables to within the compilation unit they are defined in, which I already knew. I was hoping for affirmation that in this particular case the professor's question is nonsensical given that there is only one compilation unit. – oblivioncth Dec 05 '20 at 03:06

0 Answers0