473,414 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Grouping global variables


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?

Feb 15 '07 #1
6 1559
On Feb 15, 11:15 am, "Dilip" <rdil...@lycos.comwrote:
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?

Feb 15 '07 #2
Dilip wrote:
On Feb 15, 11:15 am, "Dilip" <rdil...@lycos.comwrote:
>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
Feb 15 '07 #3
On 15 Feb, 18:46, "Dilip" <rdil...@lycos.comwrote:
On Feb 15, 11:15 am, "Dilip" <rdil...@lycos.comwrote:
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

Feb 15 '07 #4
On Feb 15, 1:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Dilip wrote:
On Feb 15, 11:15 am, "Dilip" <rdil...@lycos.comwrote:
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!

Feb 15 '07 #5
On 15 Feb, 19:16, "Gavin Deane" <deane_ga...@hotmail.comwrote:
// 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
Feb 15 '07 #6
On 15 Feb, 19:25, "Gavin Deane" <deane_ga...@hotmail.comwrote:
... 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

Feb 15 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.