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 prob

Global variable prob

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
14 Posts 4 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.
  • C Christian Graus

    Global variables are almost always a bad idea. You're better off creating a class with a static public variable. The main reason is that you have some ability to control/track access of the variable that way. The easiest way to add a global is to declare it in your stdafx.cpp. You may need to declare it in stdafx.h, but I'm pretty sure from memory that stdafx.cpp is the place to make it visible across your app. Christian Graus - Microsoft MVP - C++

    W Offline
    W Offline
    Weiye Chen
    wrote on last edited by
    #4

    Christian Graus wrote: The easiest way to add a global is to declare it in your stdafx.cpp. You may need to declare it in stdafx.h, but I'm pretty sure from memory that stdafx.cpp is the place to make it visible across your app. Yes you are right. Create the global variable in stdafx.cpp and the extern part in stdafx.h and bingo! :) Weiye Chen Life is hard, yet we are made of flesh...

    1 Reply Last reply
    0
    • W Weiye Chen

      You can put your definition of your structure in any header file. To create a global variable for your struct, you can declare it in a source file. When you need to access this in another file, declare it again but with the extern keyword. E.g.

      // File1.h
      struct MY_STRUCT {
      ...};

      // GlobalVariable.cpp
      #include "File1.h"

      MY_STRUCT myStruct;

      // AnotherFile.cpp
      #include "File1.h"

      extern MY_STRUCT myStruct;

      Weiye Chen Life is hard, yet we are made of flesh...

      B Offline
      B Offline
      benjnp
      wrote on last edited by
      #5

      I thought redeclaring the struct will reset all its values or perhaps reinitialize them.

      H 1 Reply Last reply
      0
      • C Christian Graus

        Global variables are almost always a bad idea. You're better off creating a class with a static public variable. The main reason is that you have some ability to control/track access of the variable that way. The easiest way to add a global is to declare it in your stdafx.cpp. You may need to declare it in stdafx.h, but I'm pretty sure from memory that stdafx.cpp is the place to make it visible across your app. Christian Graus - Microsoft MVP - C++

        B Offline
        B Offline
        benjnp
        wrote on last edited by
        #6

        so would it mean that I'll just have to create my struct inside a class?

        C 1 Reply Last reply
        0
        • B benjnp

          so would it mean that I'll just have to create my struct inside a class?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #7

          If you were going to have a global as a static property with get and set methods in a class, then yes, you'd define the struct in the same header file, before you defined the class. Or define it in the CPP and forward declare it, if you choose to store it as a pointer. If you put it in stdafx, then you define it in stdafx.h, I would have thought. Christian Graus - Microsoft MVP - C++

          B 2 Replies Last reply
          0
          • C Christian Graus

            If you were going to have a global as a static property with get and set methods in a class, then yes, you'd define the struct in the same header file, before you defined the class. Or define it in the CPP and forward declare it, if you choose to store it as a pointer. If you put it in stdafx, then you define it in stdafx.h, I would have thought. Christian Graus - Microsoft MVP - C++

            B Offline
            B Offline
            benjnp
            wrote on last edited by
            #8

            I guess i'll just have to do it in stdafx.cpp and stdafx.h., thanx :)

            C 1 Reply Last reply
            0
            • B benjnp

              I guess i'll just have to do it in stdafx.cpp and stdafx.h., thanx :)

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #9

              Well, you can do it either way, but doing it in a class is definately better, and no harder. Christian Graus - Microsoft MVP - C++

              1 Reply Last reply
              0
              • C Christian Graus

                If you were going to have a global as a static property with get and set methods in a class, then yes, you'd define the struct in the same header file, before you defined the class. Or define it in the CPP and forward declare it, if you choose to store it as a pointer. If you put it in stdafx, then you define it in stdafx.h, I would have thought. Christian Graus - Microsoft MVP - C++

                B Offline
                B Offline
                benjnp
                wrote on last edited by
                #10

                I've placed the struct on stdafx.cpp then the extern part on stdafx.h. But 'undeclared identifier' error still occurs

                B 1 Reply Last reply
                0
                • B benjnp

                  I've placed the struct on stdafx.cpp then the extern part on stdafx.h. But 'undeclared identifier' error still occurs

                  B Offline
                  B Offline
                  benjnp
                  wrote on last edited by
                  #11

                  oh.. undeclared identifier is no longer the error. The error now is at the stdafx.h. I guess I've placed it in the wrong area. Where am I exactly allowed to place the externs? Sorry for the disturbance

                  W 1 Reply Last reply
                  0
                  • B benjnp

                    oh.. undeclared identifier is no longer the error. The error now is at the stdafx.h. I guess I've placed it in the wrong area. Where am I exactly allowed to place the externs? Sorry for the disturbance

                    W Offline
                    W Offline
                    Weiye Chen
                    wrote on last edited by
                    #12

                    benjnp wrote: The error now is at the stdafx.h. What error? benjnp wrote: Where am I exactly allowed to place the externs? After those includes Weiye Chen Life is hard, yet we are made of flesh...

                    B 1 Reply Last reply
                    0
                    • B benjnp

                      I thought redeclaring the struct will reset all its values or perhaps reinitialize them.

                      H Offline
                      H Offline
                      HumanOsc
                      wrote on last edited by
                      #13

                      Hello... benjnp wrote: I thought redeclaring the struct will reset all its values or perhaps reinitialize them. No, only without the extern keyword... With the extern keyword you can declare global variables over many files without to reset them... They will be all associated to the first declaration... :)

                      1 Reply Last reply
                      0
                      • W Weiye Chen

                        benjnp wrote: The error now is at the stdafx.h. What error? benjnp wrote: Where am I exactly allowed to place the externs? After those includes Weiye Chen Life is hard, yet we are made of flesh...

                        B Offline
                        B Offline
                        benjnp
                        wrote on last edited by
                        #14

                        k thanx :)

                        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