1

I am trying to create bunch of void methods and call them later in program. I will demonstrate the code ahead to help better understand my issue.

.h file
static float sfloat;
namespace someNamespace
{
static void foo();
}
.cpp file
void someNamespace::foo(){cout<<sfloat<<endl}
  • above code is simpler version of class that I was working on.

I initialize sfloat in other .cpp file

otherFile.cpp
void initializeAndUseFoo(){sfloat = 5; someNamespace::foo();}

As far as my understanding goes, I expect foo to print out 5 but it prints out 0 instead. This behavior occurs across all other static variables that I have as well(pointer included). It seemed that somehow the variables inside the function are never initialized as the value I assign.

However if I call out "sfloat" not through the function then I can call it out properly.(if I just print it out on console with just

cout<<"just print it not through the function : " <<sfloat<<endl;

then it is indeed 5

Thank you for reading.

BlueBug
  • 369
  • 4
  • 17
  • 5
    `static` when used on global variables creates a copy of that variable per TU. – Necrolis Oct 18 '12 at 07:23
  • could u please if you are kind enough explain what you mean by TU? – BlueBug Oct 18 '12 at 07:26
  • 1
    @user1217203 ...a copy per Translation Unit. So there will be duplicates. You need one static variable in one Translation Unit. Copy that statement into one `.cpp` file, and change the original to say `extern` instead of `static`. Then the compiler will know not to redefine the variable and the linker will know to look for the single translation unit with the static defined. – Peter Wood Oct 18 '12 at 07:31
  • Thank u peter wood i ll try to use that approach :) – BlueBug Oct 18 '12 at 07:49

2 Answers2

4

A global static variable is static within one compilation unit. If you make another compilation unit, it will have a separate copy of that static variable.

This SO question explains what a compilation unit is in C++.

Since your variable is static and global in the same time, there will be a single instance of it in it's compilation unit. If you want to access that exact variable from another file, you will have to use the extern keyword. Without it, a new copy of that variable will be created in each compilation unit. You can find some information here, for example.

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • could u elaborate what you mean by "compilation unit" plus why would it be different from the first one? I mean it's static....? so I was expecting to have one variable named sfloat? It's not like I am creating a new one every time? or am I? – BlueBug Oct 18 '12 at 07:25
  • 1
    @user1217203 Yes, you're creating one everywhere the header is included. – Peter Wood Oct 18 '12 at 07:32
  • 1
    @user1217203 Simply put, a .cpp file. – Christian Rau Oct 18 '12 at 07:43
  • Thank u singer petrr and christian. I really appreicate all of u guys <3 – BlueBug Oct 18 '12 at 07:48
  • I chose this as an answer because he not only provides a great insightful but also a direction to rich my understanding. I wish i could choose both as an answer though. Ty fellas – BlueBug Oct 18 '12 at 07:52
3

That's not a class, it's a namespace. static inside the namespace or at global scope gives methods and variables internal linkage. That means a copy of each will be available for each translation unit.

Because you modify (not initialize) sfloat in otherFile.cpp, only that version of the variable is modified. The original, initialized in .cpp file, retains the same value (which is the version printed by someNamespace::foo().

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Thank you so much for explaining such an unexpected behavior. My laptop is about to die so I can't really detail how greatful I am. However I would appreciate so much if you could hint me how then not "modify" but initialize it with a value that I want to initialize it with( I need to initialize the variable with the value from other class ) or if namespace exhibits such a weird behavior should I be better off using class instead? like M – BlueBug Oct 18 '12 at 07:32
  • Math library? make it like static class? – BlueBug Oct 18 '12 at 07:32
  • @user1217203 well, actually, a "global" static (not member) would be initialized when declared. – Luchian Grigore Oct 18 '12 at 07:47
  • @user1217203 Hard to say what is the best approach, since your example usage is kind of weird, anyway. There is (or should be) usually no need for global variables. And templates (which I'm pretty sure you don't need/want) should be the only reason to use a class with all static methods instead of a namespace. Also, *SingerOfTheFall* gives a simple way to make your global variable *"unique"* in his answer. – Christian Rau Oct 18 '12 at 07:50
  • I like ur name christian rau hehehe. This free learning opportunity really delights me. I wish i was as able as like some other programmers so that i can share and lead as well .:) – BlueBug Oct 18 '12 at 07:56