473,890 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1572
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::s omething_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::someth ing_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...@com Acast.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::s omething_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...@ho tmail.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::someth ing_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...@ho tmail.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::someth ing_else = 1;
// end globals.cpp

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

int main()
{
int foo_in_main = globals::x;
short bar_in_main = globals::someth ing_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
3074
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 been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
9
8678
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 Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
5
11840
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 $MYVAR, $a; ?>
1
29416
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
9978
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10810
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10918
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8015
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4674
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3275
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.