global objects, C++ style

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I am new to C++ from C, I am a bit confused about using global objects in
C++. I have a program that need to share some data(held in classes) between
files, and of course I thought about using global objects. But then I notice
lots of people said it is something to avoid doing. Can someone tell me where
to draw the line to use them or not?

Thanks in advance.
 
MechSoft said:
Hi, I am new to C++ from C, I am a bit confused about using global objects
in
C++. I have a program that need to share some data(held in classes)
between
files, and of course I thought about using global objects. But then I
notice
lots of people said it is something to avoid doing. Can someone tell me
where
to draw the line to use them or not?

Globals can make a program hard to reason about.

For example, if you have a function, and if the behavior of that function
depends on its parameters that's one thing. But if its behavior also depends
on global variables which are themselves changed in far-flung locations in
your application then it becomes much more difficult to analyze that
function and predict its behavior.

Some here might tell you never to use them. That's not bad advice.

I tend to use them sparingly for truly static quantities, truly global to an
application, which once set, don't ever change and don't require
synchronized access. But then, I'm an old hacker. :-)

For example, in a windowed application, I'll often keep the handle to the
application's main window in a global variable. Of course, in a function
that I intend to share across projects, the window handle will be a
parameter.

Regards,
Will
 
Thanks, Will, for your kind explaination.

In my case, I was thinking of a data pool (status, global configuration
data), which different modules of my program will have access to, some may
update the data, some may only retrive the data for various purposes, some
might read and write to the data pool. If not using global objects, what do
you think would be the best solution to it?

Thanks and regards,

Cate
 
Just jumping in with a comment. If you do decide to use global variables, I
recommend putting them in their own namespace. That way any naming
conflicts will be resolved and/or easy to discover...

[==Peteroid ==]
 
MechSoft said:
Thanks, Will, for your kind explaination.

You are welcome.
In my case, I was thinking of a data pool (status, global configuration
data), which different modules of my program will have access to, some may
update the data, some may only retrive the data for various purposes, some
might read and write to the data pool. If not using global objects, what
do
you think would be the best solution to it?

Well, the devil is _always_ in the details. The data pool sounds like it
might be implemented as a class with methods to query and modify the data.
Rather than a global, you might want to look at the "singleton pattern". It
is what you use when you need exactly one instance of a class.

Just by the way, theses groups are a good source of information on specific
topics, especially when you have a specific question. And if you haven't
heard the terms "singleton" or "pattern" before it can serve as something to
start a search on google which eventually winds up in Barnes and Noble or
Borders or the bookstore at a local university. But there is no substitute
for quiet research and experimentation.

Regards,
Will
 
Well, the devil is _always_ in the details. The data pool sounds like it
might be implemented as a class with methods to query and modify the data.

Yes, only use methods to change and query data.
Rather than a global, you might want to look at the "singleton pattern". It
is what you use when you need exactly one instance of a class.

Just by the way, theses groups are a good source of information on specific
topics, especially when you have a specific question. And if you haven't
heard the terms "singleton" or "pattern" before it can serve as something to
start a search on google which eventually winds up in Barnes and Noble or
Borders or the bookstore at a local university. But there is no substitute
for quiet research and experimentation.

Thank you very much. I did some reading after reading your post and founf
this topic very interesting. You have been of great help to my issue. Thanks
a lot!

Regards,

Cate
 
Thank you for your kind suggestion.
Regards,
Cate

Peteroid said:
Just jumping in with a comment. If you do decide to use global variables, I
recommend putting them in their own namespace. That way any naming
conflicts will be resolved and/or easy to discover...

[==Peteroid ==]

MechSoft said:
Thanks, Will, for your kind explaination.

In my case, I was thinking of a data pool (status, global configuration
data), which different modules of my program will have access to, some may
update the data, some may only retrive the data for various purposes, some
might read and write to the data pool. If not using global objects, what
do
you think would be the best solution to it?

Thanks and regards,

Cate
 
Back
Top