Grouping global variables

D

Dilip

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?
 
D

Dilip

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?
 
V

Victor Bazarov

Dilip said:
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
 
G

Gavin Deane

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
 
D

Dilip

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.
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!
 
G

Gavin Deane

// 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
 
G

Gavin Deane

... 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top