473,847 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global objects, C++ style

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.
Nov 17 '05 #1
6 1352
"MechSoft" <me******@discu ssions.microsof t.com> wrote in message
news:A3******** *************** ***********@mic rosoft.com...
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


Nov 17 '05 #2
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

"William DePalo [MVP VC++]" wrote:
"MechSoft" <me******@discu ssions.microsof t.com> wrote in message
news:A3******** *************** ***********@mic rosoft.com...
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


Nov 17 '05 #3
Just jumping in with a comment. If you do decide to use global variables, I
recommend putting them in their own namespace[s]. That way any naming
conflicts will be resolved and/or easy to discover...

[==Peteroid ==]

"MechSoft" <me******@discu ssions.microsof t.com> wrote in message
news:F4******** *************** ***********@mic rosoft.com...
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

"William DePalo [MVP VC++]" wrote:
"MechSoft" <me******@discu ssions.microsof t.com> wrote in message
news:A3******** *************** ***********@mic rosoft.com...
> 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


Nov 17 '05 #4
"MechSoft" <me******@discu ssions.microsof t.com> wrote in message
news:F4******** *************** ***********@mic rosoft.com...
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
Nov 17 '05 #5
> 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

Nov 17 '05 #6
Thank you for your kind suggestion.
Regards,
Cate

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

[==Peteroid ==]

"MechSoft" <me******@discu ssions.microsof t.com> wrote in message
news:F4******** *************** ***********@mic rosoft.com...
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

"William DePalo [MVP VC++]" wrote:
"MechSoft" <me******@discu ssions.microsof t.com> wrote in message
news:A3******** *************** ***********@mic rosoft.com...
> 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



Nov 17 '05 #7

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

Similar topics

88
5175
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from the days before PHP got object-oriented features. For instance we currently have:
9
1488
by: Tian | last post by:
I want to create a object directory called Context in my program, which is based on a dict to save and retrieve values/objects by string-type name. I have the definition like this: utils.py -------------------- global sysctx class Context: def __init__(self):
6
2030
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following code: #def put_file(file_id, delete=False): # """ Function to put the file on the FTP Server # """ # print " FTP for this file started"
5
5330
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
8
2564
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
7
3150
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
15
2475
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for objects that the app used to speed up some global level data access and functionality. I have recoded the class libraries in .net and would like to acomplish the same functionality in asp.net.
5
4943
by: dave | last post by:
If I have a class that hold, for instance, user settings that should be accessible to the entire program logic, what is a good paradigm to use? In C++, I would have made it a global object, protected if necessary for thread safety. In C#, of course, there are no global objects. The Program object is static, so cannot contain object instances. While I could store it in my main form pass it around, that seems cumbersome. I could do a...
112
5506
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
9887
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
9730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10983
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...
1
10707
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
10341
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
7883
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
7056
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5719
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.