Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What's the best way to define global variable

Reply
Thread Tools

What's the best way to define global variable

 
 
David WOO
Guest
Posts: n/a
 
      03-01-2004
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.


 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      03-01-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Buster
Guest
Posts: n/a
 
      03-01-2004
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. 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.
 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      03-01-2004
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
> (E-Mail Removed)

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      03-02-2004
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
(E-Mail Removed)
 
Reply With Quote
 
Gary
Guest
Posts: n/a
 
      03-02-2004
"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      03-02-2004
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:
>
> 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
> (E-Mail Removed)

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      03-02-2004
Julie wrote:

> 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
>>(E-Mail Removed)


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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      03-02-2004

"Thomas Matthews" <(E-Mail Removed)> wrote in message news:Jx31c.54374$
> 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.

 
Reply With Quote
 
David WOO
Guest
Posts: n/a
 
      03-03-2004
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I define virtual global variable in ruby? femto Zheng Ruby 3 12-15-2010 03:08 PM
global typedefs and #define /\ global inheritance base eiji C++ 5 12-06-2005 12:19 AM
Best way to use Global variable bnob ASP .Net 2 12-09-2004 05:40 PM
newbiw question: What is the best way to declare and define global variables? david C Programming 1 10-06-2004 04:04 PM
how to define global variable in main() Davor C Programming 17 07-06-2003 12:27 AM



Advertisments