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. Global Explicit Static Variables

Global Explicit Static Variables

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 3 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.
  • S Offline
    S Offline
    switang
    wrote on last edited by
    #1

    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?

    M L 2 Replies Last reply
    0
    • S switang

      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?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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;

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        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;

        S Offline
        S Offline
        switang
        wrote on last edited by
        #3

        That worked. Thanks! Had to use just: bool booleanValue = true; in cpp file. So should this be done for functions as well?

        M 1 Reply Last reply
        0
        • S switang

          That worked. Thanks! Had to use just: bool booleanValue = true; in cpp file. So should this be done for functions as well?

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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

          S 1 Reply Last reply
          0
          • M Mark Salsbery

            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

            S Offline
            S Offline
            switang
            wrote on last edited by
            #5

            Thanks, ...Learn'n me some'm Now I am a C++ Master...:laugh:

            1 Reply Last reply
            0
            • S switang

              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?

              L Offline
              L Offline
              lafleon
              wrote on last edited by
              #6

              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

              S 1 Reply Last reply
              0
              • L lafleon

                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

                S Offline
                S Offline
                switang
                wrote on last edited by
                #7

                Thanks. Never thought to do it that way...thanks again.

                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