February 27th, 2004, 03:26 AM
accessing variable that belongs to aother class
I have a member variable in the class CSpanaView,called ZoomIndicator. I want to access that variable in aother class CFastZoom. Below is the code by me to do above task
but there is a memory exception when i run the code above
ZoomIndicator is a bool
Please help me!Thank You!
February 27th, 2004, 03:32 AM
Re: accessing variable that belongs to aother class
You cannot access a private member variable in CSpanaView class like this.
You can access ZoonIndicator through the member functions of CSpanaView if it is a private member variable.
Last edited by joscollin; February 27th, 2004 at 03:40 AM.
February 27th, 2004, 03:35 AM
February 27th, 2004, 03:42 AM
You need to assign a valid address to *ptSpana before you can dereference it. Since you didn't assign any value to *ptSpana in its declaration, it is pointing to an invalid address. As a result, when you dereference it, memory exception occurs.
February 27th, 2004, 03:45 AM
well prior to even touching the private / public issue i will point out that
will always be invalid because you never actually create an object or otherwise assign the pointer. without more info on your code though i wont provide any more insight on how you should solve this except to say that you shouldn't access even public members VARIABLES in my opinion (i consider public variables bad design, there should be a member function that returns/sets the variable or something) but that's just my opinion and there are exceptions
i see kheun has just posted the same point while i was typing it up. my apologies.
Last edited by filthy_mcnasty; February 27th, 2004 at 03:47 AM.
February 27th, 2004, 03:46 AM
Yes. What Kheun said is correct.
You can allocate memory for an object and assign its address to
CSpanaView *ptSpana = new CSpanaView(......
It's better ZoomIndicator to be a private variable and access it through your operator functions in CSpanaView class.
Last edited by joscollin; February 27th, 2004 at 03:53 AM.
Click Here to Expand Forum to Full Width