dunno
11-21-2000, 07:04 PM
Is there an alternative to using global variables?
------------------
"I'll get enough sleep when I'm dead."
--Left Behind, The Movie
------------------
"I'll get enough sleep when I'm dead."
--Left Behind, The Movie
|
Click to See Complete Forum and Search --> : Global Variables dunno 11-21-2000, 07:04 PM Is there an alternative to using global variables? ------------------ "I'll get enough sleep when I'm dead." --Left Behind, The Movie Larkfellow 11-21-2000, 07:16 PM Originally posted by dunno: Is there an alternative to using global variables? Yes... but it depends on what language you're using.. most languages, such as C and C++ you can pass by reference or pointers which will do the same trick for you and you don't need to have a global variable. dunno 11-21-2000, 07:24 PM C++, I want to create an unknown amount of variables though... ------------------ "I'll get enough sleep when I'm dead." --Left Behind, The Movie Dru Lee Parsec 11-21-2000, 07:29 PM Global Variables !!?? How dare you mention those EVIL VILE things in polite company. There's always an alternative to globals. What is it exactly you want to do? Maybe we can design an architecture or you that will get the job done and avoid those evil nasty things that I will not mention. [This message has been edited by Dru Lee Parsec (edited 21 November 2000).] Stuka 11-21-2000, 07:37 PM Well said Dru! I almost cried when I saw the title of the post. YES, there is almost always (if not always, especially in C++!) a solution to avoid global variables. What are you trying to do? What kind of variable do you need to create? What is it going to do? Dru Lee Parsec 11-21-2000, 11:53 PM Stuka speaks the truth. In an object oriented language there is as little use for global variables as there is for a GOTO. However, a good singleton class can be very useful, and a lot of people use singletons like they were globals. BTW, there is a great book called "Code Complete" that discusses the "why" behind these opinions. I had one Sr. programmer tell me that every coder should read that book once a year. dunno 11-22-2000, 04:11 PM I'm going to have an unknown amount of threads that need to all have access to an unknown amount of variables. But the only thing I can think of is a known amount of global variables that the threads can access. I really don't want to do that. ------------------ "I'll get enough sleep when I'm dead." --Left Behind, The Movie Stuka 11-22-2000, 05:12 PM dunno- What kind of variables are these? One possible method would be to create a singleton class with a dynamically adjustable array (or an STL list, or something of the sort) as a member, and methods to access the items on the list/array. If the variables are more complex, then some sort of object would be appropriate. What are the vars, and what do your threads need to do with them? dunno 11-22-2000, 09:20 PM The variables would be a changeable number of objects and the threads need to be able to use the object's methods. What is this singleton class idea? and how do you make an array without a fixed length? If I could do that I could probably solve my problem. ------------------ "I'll get enough sleep when I'm dead." --Left Behind, The Movie Strike 11-23-2000, 02:44 PM Originally posted by dunno: and how do you make an array without a fixed length? Create a pointer to that data type and allocate/deallocate (malloc() and free(), I think it is) memory every time you need some. That's the C version of dynamic memory allocation, if you don't want to deal with class/STL stuff. dunno 11-23-2000, 02:50 PM Well, what is the class/STL stuff? Stuka 11-23-2000, 05:07 PM OK - in the STL, you have several different template classes that allow you to have variable length arrays - vector, list, and deque - or hashes - map and set. You declare a list, for example by including <list>, then you would say list<myObject> ObjectList; and then filling it using push_front(), push_back(), etc. I don't have a reference w/me (I'm at my parents' house for Thanksgiving), but that is some of the basic stuff. To traverse the list, you use a list::iterator MyIterator; You can treat this just like a pointer (i.e. dereference, etc.) but it's better - because a lot of the list class functions return iterators (like list::begin(), list::end() for example). Beats the heck out of global variables! justlinux.com
Copyright Internet.com Inc. All Rights Reserved. |