Grouping global variables

Discussion in 'C++' started by Dilip, Feb 15, 2007.

  1. Dilip

    Dilip Guest

    I know global variables are frowned upon in C++ but suppose you are
    not in a position to decide (due to influx of legacy code etc) is
    there a recommended way to group them all (or logically) together for
    application-wide use? Like dumping them into an anonymous namespace
    or something like that?
    Dilip, Feb 15, 2007
    #1
    1. Advertising

  2. Dilip

    Dilip Guest

    On Feb 15, 11:15 am, "Dilip" <> wrote:
    > I know global variables are frowned upon in C++ but suppose you are
    > not in a position to decide (due to influx of legacy code etc) is
    > there a recommended way to group them all (or logically) together for
    > application-wide use? Like dumping them into an anonymous namespace
    > or something like that?


    OK. After some looking around it looks like people do it this way:

    // globals.h
    typedef struct globalStruct
    {
    int x;
    short something_else;
    } GlobalStruct;

    extern GlobalStruct* gs;

    // globals.cpp
    GlobalStruct* gs;
    static GlobalStruct st_gs;

    void globals_init()
    {
    gs = &st_gs;
    }

    // somewhere in main.cpp
    int main()
    {
    globals_init();
    return 0;
    }

    Is this the right way to go about doing it?
    Dilip, Feb 15, 2007
    #2
    1. Advertising

  3. Dilip wrote:
    > On Feb 15, 11:15 am, "Dilip" <> wrote:
    >> I know global variables are frowned upon in C++ but suppose you are
    >> not in a position to decide (due to influx of legacy code etc) is
    >> there a recommended way to group them all (or logically) together for
    >> application-wide use? Like dumping them into an anonymous namespace
    >> or something like that?

    >
    > OK. After some looking around it looks like people do it this way:
    >
    > // globals.h
    > typedef struct globalStruct
    > {
    > int x;
    > short something_else;
    > } GlobalStruct;


    OK... People do NOT do it this way in C++. In *C++* people do

    struct GlobalStruct
    {
    int x;
    short something_else;
    };

    The 'typedef' dance is only needed in C and only if you want to
    use 'GlobalStruct' without the keyword 'struct' in front of it.

    >
    > extern GlobalStruct* gs;
    >
    > // globals.cpp
    > GlobalStruct* gs;
    > static GlobalStruct st_gs;


    Why have a pointer along with the object. Why not simply have
    the object? Also, if you really want something *global*, I would
    still prefer

    struct GlobalStruct
    {
    static int x;
    static short something_else;
    };

    (in the header), and then

    int GlobalStruct::x;
    short GlobalStruct::something_else;

    in your 'globals.cpp' file. At least then you can access the
    variables using true "global" syntax:

    int b = GlobalStruct::x;

    instead of the obscure

    int b = gs->x;

    >
    > void globals_init()
    > {
    > gs = &st_gs;
    > }
    >
    > // somewhere in main.cpp
    > int main()
    > {
    > globals_init();
    > return 0;
    > }
    >
    > Is this the right way to go about doing it?


    Whatever helps you achieve what you set out to achieve.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 15, 2007
    #3
  4. Dilip

    Gavin Deane Guest

    On 15 Feb, 18:46, "Dilip" <> wrote:
    > On Feb 15, 11:15 am, "Dilip" <> wrote:
    >
    > > I know global variables are frowned upon in C++ but suppose you are
    > > not in a position to decide (due to influx of legacy code etc) is
    > > there a recommended way to group them all (or logically) together for
    > > application-wide use? Like dumping them into an anonymous namespace
    > > or something like that?

    >
    > OK. After some looking around it looks like people do it this way:
    >
    > // globals.h
    > typedef struct globalStruct
    > {
    > int x;
    > short something_else;
    >
    > } GlobalStruct;
    >
    > extern GlobalStruct* gs;
    >
    > // globals.cpp
    > GlobalStruct* gs;
    > static GlobalStruct st_gs;
    >
    > void globals_init()
    > {
    > gs = &st_gs;
    >
    > }
    >
    > // somewhere in main.cpp
    > int main()
    > {
    > globals_init();
    > return 0;
    >
    > }
    >
    > Is this the right way to go about doing it?


    It seems a lot more cumbersome to me than:

    // globals.h
    namespace globals
    {
    int x;
    short something_else;
    }
    // end globals.h

    // main.cpp
    #include "globals.h"

    int main()
    {
    int foo = globals::x;
    short bar = globals::something_else;
    }
    // end main.cpp

    That's what I'd do if I was stuck with a set of globals to manage. You
    can include globals.h in every source file that needs it.

    Gavin Deane
    Gavin Deane, Feb 15, 2007
    #4
  5. Dilip

    Dilip Guest

    On Feb 15, 1:08 pm, "Victor Bazarov" <> wrote:
    > Dilip wrote:
    > > On Feb 15, 11:15 am, "Dilip" <> wrote:
    > > OK. After some looking around it looks like people do it this way:

    >
    > > // globals.h
    > > typedef struct globalStruct
    > > {
    > > int x;
    > > short something_else;
    > > } GlobalStruct;

    >
    > OK... People do NOT do it this way in C++. In *C++* people do
    >
    > struct GlobalStruct
    > {
    > int x;
    > short something_else;
    > };
    >
    > The 'typedef' dance is only needed in C and only if you want to
    > use 'GlobalStruct' without the keyword 'struct' in front of it.


    You have to forgive me here. Although I didn't mention this, where I
    work, they make it mandatory to declare structures with a typedef. I
    have tried but it seems to me that if they feel a particular way of
    doing something is more idealogical than actually yielding any
    practical benefits, anything I say just falls into deaf ears.

    >
    > > extern GlobalStruct* gs;

    >
    > > // globals.cpp
    > > GlobalStruct* gs;
    > > static GlobalStruct st_gs;

    >
    > Why have a pointer along with the object. Why not simply have
    > the object? Also, if you really want something *global*, I would
    > still prefer
    >
    > struct GlobalStruct
    > {
    > static int x;
    > static short something_else;
    > };
    >
    > (in the header), and then
    >
    > int GlobalStruct::x;
    > short GlobalStruct::something_else;
    >
    > in your 'globals.cpp' file. At least then you can access the
    > variables using true "global" syntax:
    >
    > int b = GlobalStruct::x;
    >
    > instead of the obscure
    >
    > int b = gs->x;


    This is exactly what I had in mind. The example I posted was snipped
    out of some open source code I saw on the internet. That kind of
    doing things seems to be prevalent in many OSS codebases I checked in
    my few minutes of googling. Hence my query as to what is recommended
    *C++* way of doing things.

    > Whatever helps you achieve what you set out to achieve.


    I am not an expert in C++ -- I only have an above average
    understanding of the language. Despite that most times I am always
    sitting on a fence over the **C++** way of doing things as opposed to
    plodding on with the first thing that comes to my mind.

    Hence my trivial queries!
    Dilip, Feb 15, 2007
    #5
  6. Dilip

    Gavin Deane Guest

    On 15 Feb, 19:16, "Gavin Deane" <> wrote:
    > // globals.h
    > namespace globals
    > {
    > int x;
    > short something_else;}
    >
    > // end globals.h
    >
    > // main.cpp
    > #include "globals.h"
    >
    > int main()
    > {
    > int foo = globals::x;
    > short bar = globals::something_else;}
    >
    > // end main.cpp
    >
    > That's what I'd do if I was stuck with a set of globals to manage. You
    > can include globals.h in every source file that needs it.


    .... as long as you don't mind accumulating fatal linker errors. Ignore
    the rubbish above. My brain was watching television and didn't notice
    my fingers still typing.

    Gavin Deane
    Gavin Deane, Feb 15, 2007
    #6
  7. Dilip

    Gavin Deane Guest

    On 15 Feb, 19:25, "Gavin Deane" <> wrote:

    > ... as long as you don't mind accumulating fatal linker errors. Ignore
    > the rubbish above. My brain was watching television and didn't notice
    > my fingers still typing.


    What I meant was

    // globals.h
    namespace globals
    {
    extern int x;
    extern short something_else;
    }
    // end globals.h

    // globals.cpp
    #include "globals.h"

    int globals::x = 42;
    short globals::something_else = 1;
    // end globals.cpp

    // main.cpp
    #include "globals.h"

    int main()
    {
    int foo_in_main = globals::x;
    short bar_in_main = globals::something_else;
    }
    // end main.cpp

    *Now* you can include globals.h in every source file you need it in.

    Gavin Deane
    Gavin Deane, Feb 15, 2007
    #7
    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. Wayne
    Replies:
    2
    Views:
    467
    Wayne
    Nov 11, 2003
  2. -
    Replies:
    3
    Views:
    344
    Wibble
    Jul 14, 2005
  3. jubelbrus
    Replies:
    5
    Views:
    608
    JohnQ
    Jul 20, 2007
  4. mark4asp
    Replies:
    1
    Views:
    212
  5. Tony Archer

    Global Variables? Multi-Application Variables?

    Tony Archer, Nov 24, 2003, in forum: ASP General
    Replies:
    5
    Views:
    219
    Tony Archer
    Nov 25, 2003
Loading...

Share This Page