Reputation: 3564
I have a C++ DLL with 3 classes in it. I added a static boolean variable to my "stdafx" header file (since all of my classes include it) and am trying to use it. While all of my classes see my variable, they each seem to have a different instance of it. If I set the variable to true in once class then I'll notice that it's false in another class. Is there any way that I can create a variable that can be used across all classes only within the DLL?
Upvotes: 0
Views: 3390
Reputation: 109119
Your variable is static
and you're declaring it in stdafx.h
which is included by all source files in your project. This means each translation unit will contain its own copy of the variable, which is exactly the behavior you're seeing.
To solve this problem, declare the variable in stdafx.cpp
bool MyBool = false;
Then extern
it in stdafx.h
extern bool MyBool;
Upvotes: 2
Reputation: 385144
Well, you labelled it static
, so that's what happens. Instead, label it extern
in headers and define it in one TU.
And don't modify stdafx
; it's not yours. Use your own shared headers.
Upvotes: 4