What's the best way to define global variable

Discussion in 'C++' started by David WOO, Mar 1, 2004.

  1. David WOO

    David WOO Guest

    Hi,

    I am a newbie on C++, I need to define some global variables which should be
    accessible to most classes. In the mean time, I don't won't the global
    variables be modified freely at most of these classes. I know there is a
    pattern called singleton can more or less do such a trick. I am wondering is
    this the best way to do it (regarding the convenience and safety), as this
    is such a fundamental thing, I believe most of you have a say based on your
    experiences. any comment or replay will be much appreciated.

    David.
     
    David WOO, Mar 1, 2004
    #1
    1. Advertising

  2. David WOO wrote:
    >
    > Hi,
    >
    > I am a newbie on C++, I need to define some global variables which should be
    > accessible to most classes. In the mean time, I don't won't the global
    > variables be modified freely at most of these classes. I know there is a
    > pattern called singleton can more or less do such a trick.


    A singleton surfes a completely different problem.

    > I am wondering is
    > this the best way to do it (regarding the convenience and safety),


    The best way is to not have the global variables at all.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Mar 1, 2004
    #2
    1. Advertising

  3. David WOO

    Buster Guest

    David WOO wrote:
    > Hi,
    >
    > I am a newbie on C++, I need to define some global variables which should be
    > accessible to most classes. In the mean time, I don't won't the global
    > variables be modified freely at most of these classes. I know there is a
    > pattern called singleton can more or less do such a trick. I am wondering is
    > this the best way to do it (regarding the convenience and safety), as this
    > is such a fundamental thing, I believe most of you have a say based on your
    > experiences. any comment or replay will be much appreciated.
    >
    > David.
    >
    >


    Split the global data into categories. Declare a struct to store each
    category's data. In your classes, store a const reference to each struct
    whose data the class will need to access. The actual objects can be
    created in main () and passed to your class constructors. If you think
    you might accidentally instantiate any of these structs more than once,
    perhaps while you're sleeping, then the singleton pattern is for you;
    otherwise I don't think you need to bother with it.

    This isn't the best way to design classes, but I hope it helps.

    Regards,
    Buster.
     
    Buster, Mar 1, 2004
    #3
  4. David WOO

    Julie Guest

    And with a response like that, we have one less newbie to worry about.

    Karl Heinz Buchegger wrote:
    >
    > David WOO wrote:
    > >
    > > Hi,
    > >
    > > I am a newbie on C++, I need to define some global variables which should be
    > > accessible to most classes. In the mean time, I don't won't the global
    > > variables be modified freely at most of these classes. I know there is a
    > > pattern called singleton can more or less do such a trick.

    >
    > A singleton surfes a completely different problem.
    >
    > > I am wondering is
    > > this the best way to do it (regarding the convenience and safety),

    >
    > The best way is to not have the global variables at all.
    >
    > --
    > Karl Heinz Buchegger
    >
     
    Julie, Mar 1, 2004
    #4
  5. Julie wrote:
    >
    > And with a response like that, we have one less newbie to worry about.
    >


    2 things
    * please don't top post. Thank you.
    * If you know better then me, then why haven't you provided an answer
    on your own?

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Mar 2, 2004
    #5
  6. David WOO

    Gary Guest

    [OT] How to read posts (based on What's the best way to define global variable)

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    > Julie wrote:
    > >
    > > And with a response like that, we have one less newbie to worry about.
    > >

    >
    > 2 things
    > * please don't top post. Thank you.
    > * If you know better then me, then why haven't you provided an answer
    > on your own?


    When I read this post (quickly, and the first time) I said to myself: "Boy
    is Karl getting cranky. There was no need to answer like that."
    Then I read it again. After a little thought I said to myself: "Right on.
    That is surely a reasonable response to the interchange."

    What I think I learned from this is: Without the personal reactions of body
    language, facial response, voice tone, etc. just about anything written can
    be taken two ways --- as straight and helpful, or as angry and criticizing.
    It was the pause and rereading that let me see that my first impulse wasn't
    the only, and maybe not even the right, reaction.

    Being too lazy to look it up (!) I now ask: does or should the FAQ contain
    the advice to read responses that make you angry at least twice with an eye
    toward understanding what the response might mean in a more reasonable
    light? I'm sure general netiquette FAQ's do.
    --
    Gary
     
    Gary, Mar 2, 2004
    #6
  7. David WOO

    Julie Guest

    I don't recall saying that I knew better than you, perhaps you can point it out
    to me.

    I don't have an answer for the op.

    Karl Heinz Buchegger wrote:
    >
    > Julie wrote:
    > >
    > > And with a response like that, we have one less newbie to worry about.
    > >

    >
    > 2 things
    > * please don't top post. Thank you.
    > * If you know better then me, then why haven't you provided an answer
    > on your own?
    >
    > --
    > Karl Heinz Buchegger
    >
     
    Julie, Mar 2, 2004
    #7
  8. Julie wrote:

    > And with a response like that, we have one less newbie to worry about.
    >
    > Karl Heinz Buchegger wrote:
    >
    >>David WOO wrote:
    >>
    >>>Hi,
    >>>
    >>>I am a newbie on C++, I need to define some global variables which should be
    >>>accessible to most classes. In the mean time, I don't won't the global
    >>>variables be modified freely at most of these classes. I know there is a
    >>>pattern called singleton can more or less do such a trick.

    >>
    >>A singleton surfes a completely different problem.
    >>
    >>
    >>>I am wondering is
    >>>this the best way to do it (regarding the convenience and safety),

    >>
    >>The best way is to not have the global variables at all.
    >>
    >>--
    >>Karl Heinz Buchegger
    >>


    Julie,

    Karl was not insulting the OP. One of the credos of programming
    is to have no global variables. Many programmers work very hard
    to eliminate global variables. Global variables a a huge source
    of problems in large projects. Encapsulation, data hiding and
    Singletons are solutions to the problems imposed by global
    variables.

    Global variables become very dangerous in multi-tasking and
    multiprocessor applications. Search the Web for "mutex",
    "resource locking" and "signals" for some techniques.

    In some code shops, global variables must go through rigorous
    testing. The policing agencies that review the code of asked
    "have you tested every piece of code that access the variable
    to make sure there are no collisions?" That one takes a lot
    of work. After a couple of times of researching and supplying
    proof to those questions, one learns not to use globals.

    In summary, globals are evil. Most applications can be developed
    without any global variables. Those that can't are subject to
    rigorous testing.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Mar 2, 2004
    #8
  9. David WOO

    Ron Natalie Guest

    "Thomas Matthews" <> wrote in message news:Jx31c.54374$
    > Karl was not insulting the OP. One of the credos of programming
    > is to have no global variables.

    Must be one of those fundamentalist religions.
     
    Ron Natalie, Mar 2, 2004
    #9
  10. David WOO

    David WOO Guest

    As the original poster of "What's the best way to define global variable", I
    thank all of you who contributed to the post. As I started in the beginning
    of the first message that I am a newbie, As a matter of fact, I am trying to
    understand your personal views, in the meantime, I am trying to find a
    solution as well. Buster's response does give me some straight forward
    solutions when I solve simple problems, which will likely be used in my
    current work. In the meantime, I understand the point of view about "The
    best way is not have global variable at all", but it will work in the price
    of sacrifying the convenience under some situations. At the moment, I still
    can not understand the point put forward by Karl regarding "singleton surfes
    a complete different problem", as far as I understnad, the singleton can
    functions like a static class in Java, therefore, I works like a global, and
    likely it can be a solution to this problem, anyone knows about why it is
    not appropriate, could you please give some comments on this.

    Thanks again to everyone.

    David.
     
    David WOO, Mar 3, 2004
    #10
  11. David WOO

    Howie Guest

    On Mon, 1 Mar 2004 10:24:58 -0000, "David WOO" <> wrote:

    >Hi,
    >
    >I am a newbie on C++, I need to define some global variables which should be
    >accessible to most classes. In the mean time, I don't won't the global
    >variables be modified freely at most of these classes. I know there is a
    >pattern called singleton can more or less do such a trick. I am wondering is
    >this the best way to do it (regarding the convenience and safety), as this
    >is such a fundamental thing, I believe most of you have a say based on your
    >experiences. any comment or replay will be much appreciated.
    >
    >David.
    >


    I my old ways i uses global variables a lot. But to avoid them there a
    a lot of strategies to do so.
    Look in the book:
    "Large Scale C++ Software Design" from Lakos.

    Instead of global Variables i would use:

    //*.h
    class CXX
    {
    static long aGlobal;
    public:
    static void SetaGlobla(long a){ aGlobal = a;}
    static long GetaGlobal() { return aGlobal;}
    };

    //*.cpp
    long CXX::aGlobal= -1;

    ///////////////
    // use them like this:

    CXX::SetaGlobal(7);

    long x = CXX::GetaGlobla();


    Howie
     
    Howie, Mar 4, 2004
    #11
  12. David WOO wrote:
    >
    > As the original poster of "What's the best way to define global variable", I
    > thank all of you who contributed to the post. As I started in the beginning
    > of the first message that I am a newbie, As a matter of fact, I am trying to
    > understand your personal views, in the meantime, I am trying to find a
    > solution as well. Buster's response does give me some straight forward
    > solutions when I solve simple problems, which will likely be used in my
    > current work. In the meantime, I understand the point of view about "The
    > best way is not have global variable at all", but it will work in the price
    > of sacrifying the convenience under some situations.


    That's hard to believe.
    Global variables are almost always, and I really believe what I write
    here, *almost always*, a source of a problem. You can eliminate them
    easily by passing things around, at least that's what function arguments
    are for and why they were invented.

    > At the moment, I still
    > can not understand the point put forward by Karl regarding "singleton surfes
    > a complete different problem", as far as I understnad, the singleton can
    > functions like a static class in Java,


    The purpose of a singleton is to make sure, that a specific object
    exists only once (or a specific number of times) in your whole system.

    The purpose of a singleton is *not* to be a replacement of global variables.
    It can be used like that, but by doing so you have gained nothing against
    the evilness of global variables. A singleton simply doesn't solve the
    problems which make global variables evil, that is: can be accessed and,
    more important, changed by any lousy function in your system.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Mar 5, 2004
    #12
  13. David WOO

    Julie Guest

    Karl Heinz Buchegger wrote:
    > Global variables are almost always, and I really believe what I write
    > here, *almost always*, a source of a problem. You can eliminate them
    > easily by passing things around, at least that's what function arguments
    > are for and why they were invented.


    I gotta tell you that blanket statements can do much more harm than good.

    I wish that we all could get into the habit of justifying statements w/
    authoritive sources or real-world experiences.

    If you don't have sources to attribute to, say that 'in my opinion' or
    'personal experience has shown' and then go off and explain why you feel that
    way.

    ---

    In my experience, globals can be the source of problems, bugs, race conditions,
    etc. when implemented and used improperly or in a non-standard way without
    supporting documentation and pre-meditated design. One of the biggest problems
    that I've seen is when globals are used to hold read/write data -- it comes
    down to what can/should write to the global, and how are potential readers
    notified by the change? A global that changes value a lot, or is nothing more
    than a lazy way to get out of passing parameters should definitely be a cause
    for concern and a thorough code review.

    I've seen and used cases where a global (read-only) variable makes perfect
    sense, and is appropriate. For instance, some operating systems have a notion
    of a process or instance identifier that is unique for that process. That
    identifier is passed into main on program startup, but is otherwise
    unavailable. Storing the value of the process id in a global variable can be
    justified, but I'd stress that you attempt to guard it from modification by
    making access to it read-only. In this case, problems can arise if you
    inadvertantly write to it, which can be much easier than it seems if you are
    using equality such as if (global_instance = 123) when if (global_instance ==
    123) was intended. I'm sure that Karl was speaking of this type of bug which
    can be difficult at best to narrow down.

    There are numerous authorative resources out there on the subject of globals.
    Research and understand the ramifications of global now while you are still
    learning as it can be much harder to change bad habits down the road.
     
    Julie, Mar 5, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bnob

    Best way to use Global variable

    bnob, Dec 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,936
    Jeff Evans
    Dec 9, 2004
  2. Davor

    how to define global variable in main()

    Davor, Jul 2, 2003, in forum: C Programming
    Replies:
    17
    Views:
    12,215
    Mark McIntyre
    Jul 6, 2003
  3. Hendrik van Rooyen
    Replies:
    0
    Views:
    227
    Hendrik van Rooyen
    Nov 3, 2006
  4. Fredrik Lundh
    Replies:
    4
    Views:
    229
    Hendrik van Rooyen
    Nov 6, 2006
  5. eiji
    Replies:
    5
    Views:
    471
    Rolf Magnus
    Dec 6, 2005
Loading...

Share This Page