Global Variables: Safest Use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • locus
    New Member
    • Mar 2007
    • 8

    Global Variables: Safest Use

    Hi,


    I want to use a variable in two files in an application that i am writing.

    What would you suggest is the saftest way to do this, should i:

    a). Define it in one of the files it will be used in and then use the
    Code:
    extern
    keyword to make it accessible in the other file.

    b). define it in a header file and then include the header file in both the files i want to use the variable in

    If you can give any suggestions on the best way to accomplish the latter, that would be appreciated.

    All suggestions and reasons welcomed.

    Thanks in advance for your time.
  • archonmagnus
    New Member
    • Jun 2007
    • 113

    #2
    In my opinion, programming is an art, and everyone uses different brushstrokes--so to speak. But if I were doing it, I would put my global vars in the included header file. It just seems like it would be easier to remember where they were in such case for code maintenance. There are (potentially) valid reasons for both cases, but that is just my opinion.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Your choice (a) using the extern, works to a point. The extern says the variable is not in this file. Soyou declare the variable in another file and let the linker find it.

      I am currently writing an article where I have 10 cases where using a global variable will lead to disaster. Global variables are a carryover from C where the the focus was on functionality rather than data. An object program is data-centric so the individual data variables are not exposed.

      Your choice (2) won't work. You will have a global varible created each time the header is included. These duplicates will cause the linker to generate errors about redefinition.

      My suggestion:
      1) write a function that returns the appropriate value.
      2) Call that function as needed elsewhere in the program.

      This will make the value inaccessible directly and should you ever need to re-design how you obtain the value all you need to is rewrite the function and recompile the application. This is far easier than going through code and removing a global variable and replacing it with something else.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by weaknessforcats
        My suggestion:
        1) write a function that returns the appropriate value.
        2) Call that function as needed elsewhere in the program.
        If you need to set the value then write a that sets the value as well. This creates an abstract data type (a construct that in C++ has been completely replaced by classes), this is basically where you have all the data related to 1 thing as static members in 1 file and you access it to read and write values from other files by calling functions. There are a lot of benefits to this not least of which is if you want the underlying behaviour to change as long as you can maintain the same interface you only have to change the 1 file.

        Comment

        • locus
          New Member
          • Mar 2007
          • 8

          #5
          I like your first sentence archonmagnus, and i was initially leaning toward your approach,but because of your first sentence, i am going to explore what weaknessforcats and Banfa are saying. Thanks man.

          So guys let me make sure i am getting this correct;

          1). I should create a header file defining a class with functions that set and retreive the value of my variable,

          2). include this header in the two files that must have access to the value

          3). call these functions to set and retreive the value

          is this what you guys are saying?

          Thanks a lot for your responses, they are appreciated.

          Comment

          • locus
            New Member
            • Mar 2007
            • 8

            #6
            also should the functions used to set and retreive the value of the variable (the one i want to make global) be static public member functions of this class, with the variable be a private static member?
            Last edited by locus; Jul 28 '07, 09:11 PM. Reason: to make clearer

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by locus
              also should the functions used to set and retreive the value of the variable (the one i want to make global) be static public member functions of this class, with the variable be a private static member?
              I am not sure about weaknessforcats but I was initially answering from a C point of view. If you are using a class then obviously you are using C++.

              Between the 2 of us weaknessforcats is definitely stronger on C++ theory than me, however I will say this (and may be he will correct me later).

              I would generally avoid using static data for function members in a class except in the case that there is a definite possibility that there will be multiple instances of the class and those instances require some shared data (i.e. all instances using the same value for a variable).

              However you sound like what you need is actually a class that you are only going to instantiate once. There is a design pattern for this called the Singleton and weaknessforcats has written an excellent tutorial on this design pattern here. Basically in the singleton design pattern the class itself protects itself against being instantiated more than once and provides an interface that always returns the 1 instantiation of the class.

              Once you have a class that implements the singleton design pattern then you add data and function members to it in the normal way. For your purposes these members should not be static but the data members should be private or protected (so they are only accessible with the class or the class and classes that derive from it) and the functions that access or operate on the data should be public (or else you wont be able to call them).

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by Banfa
                (and may be he will correct me later).
                Nope. You are right on target.

                Comment

                Working...