-
1 Attachment(s)
extern usage
Without the static line in file1.cpp being commented out I get 300 201 300. So the extern in the function is not being used but the static variable is instead. If I do comment out the static line I get 300 301 301 which is what I expected. Why does the static declaration override the extern?
maxcy / wt1v
-
I don't have references to the standard for you, but here is my reasoning.
The extern variable declaration for "sameName" within "increment()" does not shadow the static "sameName". So, when the static global-scoped "sameName" is included in the program, "increment()" doesn't use the extern "sameName" at all.
Think about it this way. Try keeping the static "sameName" in the program, but lift the "extern" declaration of "sameName" out of "increment()" and into the global scope, as if you'd included a header file. The compiler will complain that "sameName" is declared twice: one as static and once not as static.
I don't know how local declarations of externally linked variables are supposed to interact with static definitions having the same name.