Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with global var in MFC

Problem with global var in MFC

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++debuggingquestion
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnTri
    wrote on last edited by
    #1

    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`...

    R A L P CPalliniC 5 Replies Last reply
    0
    • A AnTri

      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`...

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • A AnTri

        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`...

        A Offline
        A Offline
        amirabadi
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • R Rick York

          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.

          A Offline
          A Offline
          AnTri
          wrote on last edited by
          #4

          Thanks your for your answer. But when I delete the static keyword, I get a lot of compiler errors like (...already defined in ... object).

          S 1 Reply Last reply
          0
          • A AnTri

            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`...

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A AnTri

              Thanks your for your answer. But when I delete the static keyword, I get a lot of compiler errors like (...already defined in ... object).

              S Offline
              S Offline
              sashoalm
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • A AnTri

                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`...

                P Offline
                P Offline
                Parthiban
                wrote on last edited by
                #7

                Instead of putting as static, you can use #ifndef compiler option as follows in header file. globals.h +++++++++ #ifndef _myglobals_h #define _myglobals_h ................. declare variables ............... #endif -Parthi

                1 Reply Last reply
                0
                • A AnTri

                  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`...

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  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]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups