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 variable

Global variable

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
5 Posts 3 Posters 1 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.
  • V Offline
    V Offline
    Vickie
    wrote on last edited by
    #1

    Dear all, How can I declare a variable so that all cpps in my MFC project can access the variable? Thanks. Vickie

    C T 2 Replies Last reply
    0
    • V Vickie

      Dear all, How can I declare a variable so that all cpps in my MFC project can access the variable? Thanks. Vickie

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      in whatever.h: extern int myGlobalVariable; in whatever.cpp int myGlobalVariable = 0; in any file you want to use myGlobalVariable: #include "whatever.h" -c ------------------------------ Smaller Animals Software, Inc. http://www.smalleranimals.com

      V 1 Reply Last reply
      0
      • V Vickie

        Dear all, How can I declare a variable so that all cpps in my MFC project can access the variable? Thanks. Vickie

        T Offline
        T Offline
        Todd Wilson
        wrote on last edited by
        #3

        Like this (applies to C++ not just MFC): yourcode.h: extern globalVariableType YourGlobalVariable; yourcode.cpp: globalVariableType YourGlobalVariable; This is freqently use for making the MFC CWinApp class global like this: extern CMyApp theApp; (this is because theApp is already defined in your MyApp.cpp file by MFC). This nicely replaces: ((CMyApp*)AfxGetApp())->Bogus(); with: theApp.Bogus(); Hope this helps -------------------------------- Todd C. Wilson (tcw@nopcode.com) www.nopcode.com ICQ: 5638028 "I picked up a Magic 8-Ball the other day and it said 'Outlook not so good.' I said, 'Sure, but Microsoft still ships it.'"

        1 Reply Last reply
        0
        • C Chris Losinger

          in whatever.h: extern int myGlobalVariable; in whatever.cpp int myGlobalVariable = 0; in any file you want to use myGlobalVariable: #include "whatever.h" -c ------------------------------ Smaller Animals Software, Inc. http://www.smalleranimals.com

          V Offline
          V Offline
          Vickie
          wrote on last edited by
          #4

          I can't get it work even I have followed what you'd said, since there are many others header files included in my cpps. Will this be the reason leading to my errors? Thanks a lot!

          T 1 Reply Last reply
          0
          • V Vickie

            I can't get it work even I have followed what you'd said, since there are many others header files included in my cpps. Will this be the reason leading to my errors? Thanks a lot!

            T Offline
            T Offline
            Todd Wilson
            wrote on last edited by
            #5

            Be sure to include the header with the 'extern' wherever you are going to use it. Or stick the header include in your stdafx.h

            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