![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Globalizing member variables to avoid passing to member functions...Hello, I have programmed a class that can decode and encode JPG images.
I have made a lot of my image buffer variables global within the class to avoid having to continually pass them to member functions. I know that all I have to do is pass as pointer but still find that annoying. Please discuss the upside and downside of using this strategy. And what is the standard practice concerning this issue. One downside I was thinking of is that my buffers are MB's large so maybe having them global keeps them alive for too long stealing system resources. Thanks for any response.... Jon |
|
#2
|
||||
|
||||
Re: Globalizing member variables to avoid passing to member functions...If you are passing a lot of values into each routine, it gets almost as bad as defining all global values. So passing the pointers is at least preferable to using globals.
Globals don't really steal resources, but they allow access from anywhere in the program causing loss of control (accidentally changing a value). Making everything global also nullifies the 'power' and 'safety' of Object Oriented design. Someone more versed in OOP will probably correct me here (this is not my expertise), but if you're passing a lot of values you may not have your classes and access methods defined quite right. __________________
Age is unimportant -- except in cheese |
|
#3
|
|||
|
|||
Re: Globalizing member variables to avoid passing to member functions...Thanks for your response, it WAS helpful.
But I should be more clear. I have a lot of big buffers that are used by many member functions. Not all variables have been made global. Only those that are used by most member functions. Ciao, Ryan |
|
#4
|
||||
|
||||
Re: Globalizing member variables to avoid passing to member functions...Quote:
__________________
Age is unimportant -- except in cheese |
|
#5
|
|||
|
|||
Re: Globalizing member variables to avoid passing to member functions...Quote:
Image buffer variables in a JPG decoding/encoding class doesn't provide you with enough detail? All memory buffers are allocated at run time. I just want a general idea of how people deal with this very general issue. Sorry, but I don't think more detail is needed. When to use global member variables and when not to. Please discuss. Any discussion is usefull to me. Thanks in advance. Jon |
|
#6
|
||||
|
||||
Re: Globalizing member variables to avoid passing to member functions...Quote:
Quote:
Quote:
__________________
Age is unimportant -- except in cheese |
|
#7
|
|||
|
|||
Re: Globalizing member variables to avoid passing to member functions...Quote:
Sorry, I'm talking about global variables within one class with many member functions. Not global variables in the program. So the "global" variable is a member of the class and can't be accessed outside the class. Maybe using the term "global" has caused confusion. Sorry. Jon |
|
#8
|
|||
|
|||
Re: Globalizing member variables to avoid passing to member functions...As WaltP indicates, globals are usually a very poor design idea. However, why are you even concerned about them? Class attributes are always available to class operations.
You don't need to make any data global. CPP / C++ / C Code:
Output: Code:
...so, tell us why you need globals? :davis: |
|
#9
|
|||
|
|||
Re: Globalizing member variables to avoid passing to member functions...I apologize. I don't know why this isn't clear from my original post. I am talking about private member variables that are global ONLY with the class -- instead of creating variable within one member function then passing to other member functions with the SAME class.
|
|
#10
|
|||
|
|||
Re: Globalizing member variables to avoid passing to member functions...There is a big difference between global variables and class scope variables.. private members of a class are not global variables. And there is nothing wrong with having private variables in a class, if they are staying in memory when they are not needed then it is the programmers reponsibility to free them..
|
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| passing variables out of an iframe by url | JUNK KED | MySQL / PHP Forum | 5 | 31-Jul-2007 09:33 |
| Difficulty Passing Structure Data Elements Between Functions | jdbrine | C++ Forum | 9 | 21-May-2006 12:03 |
| Help with class, member functions and consructor | GrassPuppet | C++ Forum | 2 | 04-Feb-2006 17:34 |
| How many member functions within Thing class? | jack223 | C++ Forum | 4 | 08-Nov-2005 09:45 |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 15:41 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The