-
February 27th, 2004, 03:26 AM
#1
accessing variable that belongs to aother class
Hello
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
CFastZoom::OnCancel()
{
CSpanaView *ptSpana;
ptSpana->ZoomIndicator=FALSE;
}
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
#2
Re: accessing variable that belongs to aother class
CFastZoom::OnCancel()
{
CSpanaView *ptSpana;
ptSpana->ZoomIndicator=FALSE;
}
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
#3
-
February 27th, 2004, 03:42 AM
#4
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
#5
well prior to even touching the private / public issue i will point out that
CSpanaView *ptSpana;
ptSpana->ZoomIndicator=FALSE;
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
-edit-
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
#6
Yes. What Kheun said is correct.
You can allocate memory for an object and assign its address to
ptSpana.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|