Global Explicit Static Variables
-
I have a static boolean : static bool booleanValue= true; in my stdafx.h file. I assumed this variable would have one instance across my application. But this assumption is apparently wrong. Within different objects, this boolean has separate instances, thus voiding its use. I.E. ------------------------------ Have a class: #include "stdafx.h" class Blah { int stuff; void doStuff() { if (!booleanValue) { stuff++; } } }; Have another class" #include "stdafx.h" class AnotherClass { int stuffedPeppers; void intoTheAbyss() { if (!booleanValue) { stuffedPeppers++; } } }; ------------------------------ The addresses for the same variable are different (same thread). Where am I going wrong?
-
I have a static boolean : static bool booleanValue= true; in my stdafx.h file. I assumed this variable would have one instance across my application. But this assumption is apparently wrong. Within different objects, this boolean has separate instances, thus voiding its use. I.E. ------------------------------ Have a class: #include "stdafx.h" class Blah { int stuff; void doStuff() { if (!booleanValue) { stuff++; } } }; Have another class" #include "stdafx.h" class AnotherClass { int stuffedPeppers; void intoTheAbyss() { if (!booleanValue) { stuffedPeppers++; } } }; ------------------------------ The addresses for the same variable are different (same thread). Where am I going wrong?
There's an instance for every module which includes the stdafx.h header file. Try In stdafx.h: extern bool booleanValue; And in one cpp file: static bool booleanValue= true;
-
There's an instance for every module which includes the stdafx.h header file. Try In stdafx.h: extern bool booleanValue; And in one cpp file: static bool booleanValue= true;
-
That worked. Thanks! Had to use just: bool booleanValue = true; in cpp file. So should this be done for functions as well?
switang wrote:
Had to use just: bool booleanValue = true;
Yeah, I suppose static is redundant in that case :)
switang wrote:
So should this be done for functions as well?
Yes. Declare them in the header, define them in cpp files. Unless you need specific instances for each cpp module. In that case (file local scope) you probably wouldn't want the declaration in the header file anyway. Mark
-
switang wrote:
Had to use just: bool booleanValue = true;
Yeah, I suppose static is redundant in that case :)
switang wrote:
So should this be done for functions as well?
Yes. Declare them in the header, define them in cpp files. Unless you need specific instances for each cpp module. In that case (file local scope) you probably wouldn't want the declaration in the header file anyway. Mark
-
I have a static boolean : static bool booleanValue= true; in my stdafx.h file. I assumed this variable would have one instance across my application. But this assumption is apparently wrong. Within different objects, this boolean has separate instances, thus voiding its use. I.E. ------------------------------ Have a class: #include "stdafx.h" class Blah { int stuff; void doStuff() { if (!booleanValue) { stuff++; } } }; Have another class" #include "stdafx.h" class AnotherClass { int stuffedPeppers; void intoTheAbyss() { if (!booleanValue) { stuffedPeppers++; } } }; ------------------------------ The addresses for the same variable are different (same thread). Where am I going wrong?
Hello, If you need global variables and functions just use class with static public members like this: class GlobalWrap { public: static int i1; static double d1; static void* pv1; }; You can add constructor and destructor for initialization and memory cleaning and use it like this: GlobalWrap::i1; GlobalWrap::d1; GlobalWrap::pv1->… Regards, Leonid
-
Hello, If you need global variables and functions just use class with static public members like this: class GlobalWrap { public: static int i1; static double d1; static void* pv1; }; You can add constructor and destructor for initialization and memory cleaning and use it like this: GlobalWrap::i1; GlobalWrap::d1; GlobalWrap::pv1->… Regards, Leonid