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. Proper way to define global class objects

Proper way to define global class objects

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
7 Posts 6 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.
  • G Offline
    G Offline
    georgiek50
    wrote on last edited by
    #1

    I am having trouble with linking files due to global class objects. What is the best way to define them and not having to use extern in other header files? Will making a CPP file specifically for these global class objects work (I would try this myself but since my very long code is now cluttered and in need of re-writing I can't test it without spending a couple of days of re-writing)

    D 1 Reply Last reply
    0
    • G georgiek50

      I am having trouble with linking files due to global class objects. What is the best way to define them and not having to use extern in other header files? Will making a CPP file specifically for these global class objects work (I would try this myself but since my very long code is now cluttered and in need of re-writing I can't test it without spending a couple of days of re-writing)

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      What do you mean by "global class objects?" Are you wanting an instance of a class that is accessible in more than one file? I would think that using the extern keyword would work best here. Do you have reservations about its use?


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      G 1 Reply Last reply
      0
      • D David Crow

        What do you mean by "global class objects?" Are you wanting an instance of a class that is accessible in more than one file? I would think that using the extern keyword would work best here. Do you have reservations about its use?


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        G Offline
        G Offline
        georgiek50
        wrote on last edited by
        #3

        Yes, that's exactly what I mean. I don't have a specific problem using it but I wanted to have one file where everything global is defined so I wouldn't have to go into every file and use extern in its header. Is this possible at all?

        R A D 3 Replies Last reply
        0
        • G georgiek50

          Yes, that's exactly what I mean. I don't have a specific problem using it but I wanted to have one file where everything global is defined so I wouldn't have to go into every file and use extern in its header. Is this possible at all?

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

          yes, it is possible. Here's one way : #ifdef DECLARE_GLOBALS #define Global #else #define Global extern #endif class CMyClass { ... }; Global CMyClass g_GlobalObject; then place the definition : #define DECLARE_GLOBALS in one and ONLY one file, before it includes the header declaring CMyClass. __________________________________________ a two cent stamp short of going postal.

          1 Reply Last reply
          0
          • G georgiek50

            Yes, that's exactly what I mean. I don't have a specific problem using it but I wanted to have one file where everything global is defined so I wouldn't have to go into every file and use extern in its header. Is this possible at all?

            A Offline
            A Offline
            Antti Keskinen
            wrote on last edited by
            #5

            Use a common header file, with the extern keyword, that is included by every implementation file. For example, MFC uses 'stdafx.h' file to include all common Windows/MFC/Sockets/ODBC/Whatever header files required in all files. Of course, you need to use a pragma definition (#pragma once), otherwise you might get multiple-declaration errors. Always remember, though that having too many global objects is a sure way to invite trouble in the house. Put only those variables global that MUST be, use class members and pointers to class members elsewhere whenever possible. For example, if I have a dialog that has a list control, my database class might have a direct pointer to this control, allowing manipulation where necessary. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

            A 1 Reply Last reply
            0
            • A Antti Keskinen

              Use a common header file, with the extern keyword, that is included by every implementation file. For example, MFC uses 'stdafx.h' file to include all common Windows/MFC/Sockets/ODBC/Whatever header files required in all files. Of course, you need to use a pragma definition (#pragma once), otherwise you might get multiple-declaration errors. Always remember, though that having too many global objects is a sure way to invite trouble in the house. Put only those variables global that MUST be, use class members and pointers to class members elsewhere whenever possible. For example, if I have a dialog that has a list control, my database class might have a direct pointer to this control, allowing manipulation where necessary. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              OK, this is something that I was thinking of. Let me try to understand this. Make a header file and include the global class objects as externs, then include this header file in every cpp file that needs to use those objects. I don't understand the final step. Does it have to be clearly defined in one file as not extern? If so, is this where #pragma comes in?

              1 Reply Last reply
              0
              • G georgiek50

                Yes, that's exactly what I mean. I don't have a specific problem using it but I wanted to have one file where everything global is defined so I wouldn't have to go into every file and use extern in its header. Is this possible at all?

                D Offline
                D Offline
                David Chamberlain
                wrote on last edited by
                #7

                I generally find it problematic to put variable declarations (as opposed to definitions) into header files. The tricks suggested about using #ifdef... are really just to trick the compiler into accepting a generally ill-advised way of writing code. My suggestion is to declare your global variables in the source file where they are originally created, not in a header file, and then every other source file that needs access to the global variables needs to use extern. For these other source files, they can #include a GlobalVars.h file that simply contains all of the necessary extern statements for the global variables. The main source file does not #include GlobalVars.h, as it is the file that is declaring them. The advantage to this approach is that variables (and the access to them) is not hidden through a set of #include, #define, and #ifdef statements. You are writing exactly what you want in a straightforward way that will be less prone to error and easier to understand. While it makes no difference to the compiler, it will be more understandable to anyone reading the code. Dave "You can say that again." -- Dept. of Redundancy Dept.

                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