Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

global objects, C++ style

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MechSoft - 24 Mar 2005 02:11 GMT
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.
William DePalo [MVP VC++] - 24 Mar 2005 03:22 GMT
> Hi, I am new to C++ from C, I am a bit confused about using global objects
> in
[quoted text clipped - 5 lines]
> 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
MechSoft - 24 Mar 2005 19:03 GMT
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

> > Hi, I am new to C++ from C, I am a bit confused about using global objects
> > in
[quoted text clipped - 27 lines]
> Regards,
> Will
Peteroid - 25 Mar 2005 02:04 GMT
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 ==]

> Thanks, Will, for your kind explaination.
>
[quoted text clipped - 44 lines]
>> Regards,
>> Will
MechSoft - 27 Mar 2005 21:43 GMT
Thank you for your kind suggestion.
Regards,
Cate

> 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
[quoted text clipped - 50 lines]
> >> Regards,
> >> Will
William DePalo [MVP VC++] - 25 Mar 2005 04:30 GMT
> 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
MechSoft - 27 Mar 2005 17:03 GMT
> 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.
[quoted text clipped - 5 lines]
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.