C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What's the best way to define global variable

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
David WOO
Guest





PostPosted: Mon Mar 01, 2004 10:24 am    Post subject: What's the best way to define global variable Reply with quote



Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don't won't the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick. I am wondering is
this the best way to do it (regarding the convenience and safety), as this
is such a fundamental thing, I believe most of you have a say based on your
experiences. any comment or replay will be much appreciated.

David.


Back to top
Karl Heinz Buchegger
Guest





PostPosted: Mon Mar 01, 2004 12:43 pm    Post subject: Re: What's the best way to define global variable Reply with quote



David WOO wrote:
Quote:

Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don't won't the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick.

A singleton surfes a completely different problem.

Quote:
I am wondering is
this the best way to do it (regarding the convenience and safety),

The best way is to not have the global variables at all.

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Buster
Guest





PostPosted: Mon Mar 01, 2004 12:47 pm    Post subject: Re: What's the best way to define global variable Reply with quote



David WOO wrote:
Quote:
Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don't won't the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick. I am wondering is
this the best way to do it (regarding the convenience and safety), as this
is such a fundamental thing, I believe most of you have a say based on your
experiences. any comment or replay will be much appreciated.

David.



Split the global data into categories. Declare a struct to store each
category's data. In your classes, store a const reference to each struct
whose data the class will need to access. The actual objects can be
created in main () and passed to your class constructors. If you think
you might accidentally instantiate any of these structs more than once,
perhaps while you're sleeping, then the singleton pattern is for you;
otherwise I don't think you need to bother with it.

This isn't the best way to design classes, but I hope it helps.

Regards,
Buster.

Back to top
Julie
Guest





PostPosted: Mon Mar 01, 2004 4:32 pm    Post subject: Re: What's the best way to define global variable Reply with quote

And with a response like that, we have one less newbie to worry about.

Karl Heinz Buchegger wrote:
Quote:

David WOO wrote:

Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don't won't the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick.

A singleton surfes a completely different problem.

I am wondering is
this the best way to do it (regarding the convenience and safety),

The best way is to not have the global variables at all.

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Tue Mar 02, 2004 1:24 pm    Post subject: Re: What's the best way to define global variable Reply with quote

Julie wrote:
Quote:

And with a response like that, we have one less newbie to worry about.


2 things
* please don't top post. Thank you.
* If you know better then me, then why haven't you provided an answer
on your own?

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Gary
Guest





PostPosted: Tue Mar 02, 2004 2:27 pm    Post subject: [OT] How to read posts (based on What's the best way to defi Reply with quote

"Karl Heinz Buchegger" <kbuchegg (AT) gascad (DOT) at> wrote

Quote:
Julie wrote:

And with a response like that, we have one less newbie to worry about.


2 things
* please don't top post. Thank you.
* If you know better then me, then why haven't you provided an answer
on your own?

When I read this post (quickly, and the first time) I said to myself: "Boy
is Karl getting cranky. There was no need to answer like that."
Then I read it again. After a little thought I said to myself: "Right on.
That is surely a reasonable response to the interchange."

What I think I learned from this is: Without the personal reactions of body
language, facial response, voice tone, etc. just about anything written can
be taken two ways --- as straight and helpful, or as angry and criticizing.
It was the pause and rereading that let me see that my first impulse wasn't
the only, and maybe not even the right, reaction.

Being too lazy to look it up (!) I now ask: does or should the FAQ contain
the advice to read responses that make you angry at least twice with an eye
toward understanding what the response might mean in a more reasonable
light? I'm sure general netiquette FAQ's do.
--
Gary



Back to top
Julie
Guest





PostPosted: Tue Mar 02, 2004 4:47 pm    Post subject: Re: What's the best way to define global variable Reply with quote

I don't recall saying that I knew better than you, perhaps you can point it out
to me.

I don't have an answer for the op.

Karl Heinz Buchegger wrote:
Quote:

Julie wrote:

And with a response like that, we have one less newbie to worry about.


2 things
* please don't top post. Thank you.
* If you know better then me, then why haven't you provided an answer
on your own?

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Thomas Matthews
Guest





PostPosted: Tue Mar 02, 2004 5:31 pm    Post subject: Re: What's the best way to define global variable Reply with quote

Julie wrote:

Quote:
And with a response like that, we have one less newbie to worry about.

Karl Heinz Buchegger wrote:

David WOO wrote:

Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don't won't the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick.

A singleton surfes a completely different problem.


I am wondering is
this the best way to do it (regarding the convenience and safety),

The best way is to not have the global variables at all.

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Julie,

Karl was not insulting the OP. One of the credos of programming
is to have no global variables. Many programmers work very hard
to eliminate global variables. Global variables a a huge source
of problems in large projects. Encapsulation, data hiding and
Singletons are solutions to the problems imposed by global
variables.

Global variables become very dangerous in multi-tasking and
multiprocessor applications. Search the Web for "mutex",
"resource locking" and "signals" for some techniques.

In some code shops, global variables must go through rigorous
testing. The policing agencies that review the code of asked
"have you tested every piece of code that access the variable
to make sure there are no collisions?" That one takes a lot
of work. After a couple of times of researching and supplying
proof to those questions, one learns not to use globals.

In summary, globals are evil. Most applications can be developed
without any global variables. Those that can't are subject to
rigorous testing.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Back to top
Ron Natalie
Guest





PostPosted: Tue Mar 02, 2004 7:46 pm    Post subject: Re: What's the best way to define global variable Reply with quote


"Thomas Matthews" <Thomas_MatthewsSpitsOnSpamBots (AT) sbcglobal (DOT) net> wrote

Quote:
Karl was not insulting the OP. One of the credos of programming
is to have no global variables.
Must be one of those fundamentalist religions.



Back to top
David WOO
Guest





PostPosted: Wed Mar 03, 2004 12:13 pm    Post subject: Re: What's the best way to define global variable Reply with quote

As the original poster of "What's the best way to define global variable", I
thank all of you who contributed to the post. As I started in the beginning
of the first message that I am a newbie, As a matter of fact, I am trying to
understand your personal views, in the meantime, I am trying to find a
solution as well. Buster's response does give me some straight forward
solutions when I solve simple problems, which will likely be used in my
current work. In the meantime, I understand the point of view about "The
best way is not have global variable at all", but it will work in the price
of sacrifying the convenience under some situations. At the moment, I still
can not understand the point put forward by Karl regarding "singleton surfes
a complete different problem", as far as I understnad, the singleton can
functions like a static class in Java, therefore, I works like a global, and
likely it can be a solution to this problem, anyone knows about why it is
not appropriate, could you please give some comments on this.

Thanks again to everyone.

David.


Back to top
Howie
Guest





PostPosted: Thu Mar 04, 2004 6:57 am    Post subject: Re: What's the best way to define global variable Reply with quote

On Mon, 1 Mar 2004 10:24:58 -0000, "David WOO" <ixuw (AT) yahoo (DOT) com> wrote:

Quote:
Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don't won't the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick. I am wondering is
this the best way to do it (regarding the convenience and safety), as this
is such a fundamental thing, I believe most of you have a say based on your
experiences. any comment or replay will be much appreciated.

David.


I my old ways i uses global variables a lot. But to avoid them there a
a lot of strategies to do so.
Look in the book:
"Large Scale C++ Software Design" from Lakos.

Instead of global Variables i would use:

//*.h
class CXX
{
static long aGlobal;
public:
static void SetaGlobla(long a){ aGlobal = a;}
static long GetaGlobal() { return aGlobal;}
};

//*.cpp
long CXX::aGlobal= -1;

///////////////
// use them like this:

CXX::SetaGlobal(7);

long x = CXX::GetaGlobla();


Howie


Back to top
Karl Heinz Buchegger
Guest





PostPosted: Fri Mar 05, 2004 2:17 pm    Post subject: Re: What's the best way to define global variable Reply with quote

David WOO wrote:
Quote:

As the original poster of "What's the best way to define global variable", I
thank all of you who contributed to the post. As I started in the beginning
of the first message that I am a newbie, As a matter of fact, I am trying to
understand your personal views, in the meantime, I am trying to find a
solution as well. Buster's response does give me some straight forward
solutions when I solve simple problems, which will likely be used in my
current work. In the meantime, I understand the point of view about "The
best way is not have global variable at all", but it will work in the price
of sacrifying the convenience under some situations.

That's hard to believe.
Global variables are almost always, and I really believe what I write
here, *almost always*, a source of a problem. You can eliminate them
easily by passing things around, at least that's what function arguments
are for and why they were invented.

Quote:
At the moment, I still
can not understand the point put forward by Karl regarding "singleton surfes
a complete different problem", as far as I understnad, the singleton can
functions like a static class in Java,

The purpose of a singleton is to make sure, that a specific object
exists only once (or a specific number of times) in your whole system.

The purpose of a singleton is *not* to be a replacement of global variables.
It can be used like that, but by doing so you have gained nothing against
the evilness of global variables. A singleton simply doesn't solve the
problems which make global variables evil, that is: can be accessed and,
more important, changed by any lousy function in your system.

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Julie
Guest





PostPosted: Fri Mar 05, 2004 4:21 pm    Post subject: Re: What's the best way to define global variable Reply with quote

Karl Heinz Buchegger wrote:
Quote:
Global variables are almost always, and I really believe what I write
here, *almost always*, a source of a problem. You can eliminate them
easily by passing things around, at least that's what function arguments
are for and why they were invented.

I gotta tell you that blanket statements can do much more harm than good.

I wish that we all could get into the habit of justifying statements w/
authoritive sources or real-world experiences.

If you don't have sources to attribute to, say that 'in my opinion' or
'personal experience has shown' and then go off and explain why you feel that
way.

---

In my experience, globals can be the source of problems, bugs, race conditions,
etc. when implemented and used improperly or in a non-standard way without
supporting documentation and pre-meditated design. One of the biggest problems
that I've seen is when globals are used to hold read/write data -- it comes
down to what can/should write to the global, and how are potential readers
notified by the change? A global that changes value a lot, or is nothing more
than a lazy way to get out of passing parameters should definitely be a cause
for concern and a thorough code review.

I've seen and used cases where a global (read-only) variable makes perfect
sense, and is appropriate. For instance, some operating systems have a notion
of a process or instance identifier that is unique for that process. That
identifier is passed into main on program startup, but is otherwise
unavailable. Storing the value of the process id in a global variable can be
justified, but I'd stress that you attempt to guard it from modification by
making access to it read-only. In this case, problems can arise if you
inadvertantly write to it, which can be much easier than it seems if you are
using equality such as if (global_instance = 123) when if (global_instance ==
123) was intended. I'm sure that Karl was speaking of this type of bug which
can be difficult at best to narrow down.

There are numerous authorative resources out there on the subject of globals.
Research and understand the ramifications of global now while you are still
learning as it can be much harder to change bad habits down the road.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.