Problem with global var in MFC
-
Hallo, I tried to use some global vars to define some application wide settings. So I defined a group of global vars in my MyApp.cpp like this: MyApp.h:
#pragma once
....
static bool bSetOutputToDebugWindow;
....All my (*.cpp) files include the MyApp.h file. In all files "bSetOutputToDebugWindow" was accessable and the applicaction compiles with no error. But when I set "bSetOutputToDebugWindow" (e.g. in my Apps InitInstance-Method) to true, and tried to read the value later in debugger or function the value is false! Why???? Please help to find my error`...
-
Hallo, I tried to use some global vars to define some application wide settings. So I defined a group of global vars in my MyApp.cpp like this: MyApp.h:
#pragma once
....
static bool bSetOutputToDebugWindow;
....All my (*.cpp) files include the MyApp.h file. In all files "bSetOutputToDebugWindow" was accessable and the applicaction compiles with no error. But when I set "bSetOutputToDebugWindow" (e.g. in my Apps InitInstance-Method) to true, and tried to read the value later in debugger or function the value is false! Why???? Please help to find my error`...
-
Hallo, I tried to use some global vars to define some application wide settings. So I defined a group of global vars in my MyApp.cpp like this: MyApp.h:
#pragma once
....
static bool bSetOutputToDebugWindow;
....All my (*.cpp) files include the MyApp.h file. In all files "bSetOutputToDebugWindow" was accessable and the applicaction compiles with no error. But when I set "bSetOutputToDebugWindow" (e.g. in my Apps InitInstance-Method) to true, and tried to read the value later in debugger or function the value is false! Why???? Please help to find my error`...
I usually put the declaration (extern bool bVar; ) in the common header file and in one of the cpp files (in your case MyApp.cpp)I do create the variable ( bool bVar; ). That guarantees that I'm using the same variable across my cpp files. but personally I try to avoid using global variables. A better approach would be static member variable.
-
This happens because you said it was static. That means it there is a static instance of it in every module that includes MyApp.h. If you want the variable to be global and shared among the modules of the app then it can not be static.
-
Hallo, I tried to use some global vars to define some application wide settings. So I defined a group of global vars in my MyApp.cpp like this: MyApp.h:
#pragma once
....
static bool bSetOutputToDebugWindow;
....All my (*.cpp) files include the MyApp.h file. In all files "bSetOutputToDebugWindow" was accessable and the applicaction compiles with no error. But when I set "bSetOutputToDebugWindow" (e.g. in my Apps InitInstance-Method) to true, and tried to read the value later in debugger or function the value is false! Why???? Please help to find my error`...
You should not be, under any circumstances, declaring a variable in a header file, unless it is inside a function, class, or struct declaration. That is how you get the compile errors you were talking about when you remove the
static
keyword ;) This resource[^] may be useful to you. -
Thanks your for your answer. But when I delete the static keyword, I get a lot of compiler errors like (...already defined in ... object).
Are the globals changed at run-time? If they aren't you can substitute them with defines, for example: bool bSetOutputToDebugWindow = false; becomes #define SETOUTPUTTODEBUGWINDOW 0
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal
-
Hallo, I tried to use some global vars to define some application wide settings. So I defined a group of global vars in my MyApp.cpp like this: MyApp.h:
#pragma once
....
static bool bSetOutputToDebugWindow;
....All my (*.cpp) files include the MyApp.h file. In all files "bSetOutputToDebugWindow" was accessable and the applicaction compiles with no error. But when I set "bSetOutputToDebugWindow" (e.g. in my Apps InitInstance-Method) to true, and tried to read the value later in debugger or function the value is false! Why???? Please help to find my error`...
-
Hallo, I tried to use some global vars to define some application wide settings. So I defined a group of global vars in my MyApp.cpp like this: MyApp.h:
#pragma once
....
static bool bSetOutputToDebugWindow;
....All my (*.cpp) files include the MyApp.h file. In all files "bSetOutputToDebugWindow" was accessable and the applicaction compiles with no error. But when I set "bSetOutputToDebugWindow" (e.g. in my Apps InitInstance-Method) to true, and tried to read the value later in debugger or function the value is false! Why???? Please help to find my error`...
You should not do that. Instead: (1) declare your variable as
extern
in the (commonly included) header file, for instance in MyApp.h:extern bool bSetOutputToDebugWindow;
(2) define the variable only once, inside one source file, for instance MyApp.cpp (please note I did not use the
static
keyword):bool bSetOutputToDebugWindow;
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]