GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Aug-2006, 18:03
jondean jondean is offline
Awaiting Email Confirmation
 
Join Date: Jun 2006
Posts: 19
jondean is on a distinguished road

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  
Old 13-Aug-2006, 18:44
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

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  
Old 13-Aug-2006, 19:14
jondean jondean is offline
Awaiting Email Confirmation
 
Join Date: Jun 2006
Posts: 19
jondean is on a distinguished road

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  
Old 13-Aug-2006, 20:21
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Globalizing member variables to avoid passing to member functions...


Quote:
Originally Posted by jondean
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.
All well and good (what a weird phrase...), but without details about what the variables are, how they relate to the class, and other design issues, no one can really say your globals are needed or not. Or whether they should be in the class or not. It could be the 'buffers' can defined simply as pointers and the memory for them allocated on an as-needed basis.
__________________

Age is unimportant -- except in cheese
  #5  
Old 13-Aug-2006, 20:33
jondean jondean is offline
Awaiting Email Confirmation
 
Join Date: Jun 2006
Posts: 19
jondean is on a distinguished road

Re: Globalizing member variables to avoid passing to member functions...


Quote:
Originally Posted by WaltP
All well and good (what a weird phrase...), but without details about what the variables are, how they relate to the class, and other design issues, no one can really say your globals are needed or not. Or whether they should be in the class or not. It could be the 'buffers' can defined simply as pointers and the memory for them allocated on an as-needed basis.


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  
Old 13-Aug-2006, 21:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Globalizing member variables to avoid passing to member functions...


Quote:
Originally Posted by jondean
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.
OK...

Quote:
Originally Posted by jondean
When to use global member variables and when not to. Please discuss. Any discussion is usefull to me.
Hopefully someone else will add their opinion. My opinion is "rarely if ever", for the reasons stated before:
Quote:
Originally Posted by WaltP
Globals ... 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.
__________________

Age is unimportant -- except in cheese
  #7  
Old 13-Aug-2006, 21:27
jondean jondean is offline
Awaiting Email Confirmation
 
Join Date: Jun 2006
Posts: 19
jondean is on a distinguished road

Re: Globalizing member variables to avoid passing to member functions...


Quote:
Originally Posted by WaltP
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.

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  
Old 13-Aug-2006, 21:39
davis
 
Posts: n/a

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:
#include <iostream>

using namespace std;

class Foo
{
    public:
        Foo() : m_int( 900 ) {};
        void bar() { cout << "m_int = " << m_int << endl; };

    private:
        int m_int;
};

int main()
{
    Foo f;
    f.bar();
    return 0;
}


Output:

Code:
m_int = 900

...so, tell us why you need globals?


:davis:
  #9  
Old 13-Aug-2006, 21:44
jondean jondean is offline
Awaiting Email Confirmation
 
Join Date: Jun 2006
Posts: 19
jondean is on a distinguished road

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  
Old 13-Aug-2006, 22:05
outlaw98 outlaw98 is offline
New Member
 
Join Date: Aug 2006
Posts: 14
outlaw98 is on a distinguished road

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 GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 07:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.