Click to See Complete Forum and Search --> : Pointers
kovan
Jun 14th, 2001, 09:03 PM
i have a treeview on a class called CLeftView (both generated by mfc wizards...
CLeftView has a function called AddShape (i added this one)
and i have a class called CMainView (also generated by the wizard)
now to the question
in CMainView
i would like to call a this AddFunction
but i dont know how
i tried several ways of using pointers
and using CLeftVeiw::AddShape;
still havent got anywhere
i have also tried
auto_ptr<CLeftView> ptr;
ptr->AddShape
when this is called i get a memory access violation
me need a pointer to cleftview that doesnt give me memory access violiations
thanks
Technocrat
Jun 15th, 2001, 10:43 AM
Ok I am going to try to help you, but this is really hard to explain. So if you dont understand dont worry it took me awhile to get it at first too.
When you want to access any functions, and/or VARs that are located inside of classess, that are not expressly told that they are to be global, you have to point to that class to get access to them. If you dont you end up rebuilding the class again, which most of the time you do not want to do.
First I am going to give you an example class:
Let say I have a class called CMainDlg which is my main dialog in my app.
I have a function called PushText with makes text go to a text box on that dialog
I also have a VAR called m_txtOut which is what the current text in that text box is.
So in order to access this stuff inside of a class you first have to point to the class. In my example its CMainDlg. So to point to it I have to use (CMainDlg*)
Next I have to have the Handle of the App so the program know what App I am talking about when I use the above pointer. There a bunch of different ways to do this. I am going to give you the MFC way. AfxGetApp()->m_pMainWnd. This will give back the handle.
Now we need to merge the two together:
((CQueryDlg*)(AfxGetApp()->m_pMainWnd))
There now we have a pointer to our class. Now we can access what ever we want in that class with out rebulding it. So if I want to access the function all I have to do is:
((CQueryDlg*)(AfxGetApp()->m_pMainWnd))->PushText("WHATEVER");
If I want the VAR:
((CMainDlg*)(AfxGetApp()->m_pMainWnd))->m_txtOut
So basically it goes like this:
((NAME OF CLASS*)(AfxGetApp()->m_pMainWnd))->VAR OR FUNCTION
There, its been awhile since I have used MFC so hopefully I got everything right. If you need any more help let me know.
If anyone else comes along and reads this and knows of an easier way to do this I would like to know how.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.