-
January 13th, 2004, 09:56 AM
#1
How to access other objects public attributes?
How do I access public attributes from other objects?
I have the following situation: The function OnNMClick (from the derived CTreeCtrl class CMyTreeCtrl) is called when I click in the dialog box. That dialog (CMyDialog) has a attribute (m_data) from type CMyData (with operation change()) that I want to access in the OnNMClick function. This is necessary to change the data in CMyDialog. What I do is this:
Code:
CMyDialog::m_data.change();
But this results in the following error:
Code:
G:\MyTreeCtrl.cpp(56) : error C2228: left of '.change' must have class/struct/union type
Any help would be highly appreciated.
-
January 13th, 2004, 10:31 AM
#2
CMyDialog::m_data.change();
This fails because C++ does not know WHICH instance of CMyDialog it should use. You should give the pointer of it to you CMyTreeCtrl-class and then call:
m_pMyDialog->m_data.change();
-
January 13th, 2004, 10:50 AM
#3
That sounds really logical to me . But I do not really know a way to pass that pointer to the CMyTreeCtrl class. As far as I know, I do not (explicitly) call the constructor or any other function from CMyTreeCtrl from within CMyDialog. This all gets done mysteriously (for me ) by MFC. So what is the best way to pass along a pointer to CMyTreeCtrl?
-
January 13th, 2004, 10:57 AM
#4
Although you could indeed pass the dialog pointer to the tree control (adding an appropriate constructor or a setter method to CMyTreeCtrl), the easiest way is probably to call GetParent() from within CMyTreeCtrl() and cast the result (a CWnd*) to CMyDialog (ideally, after checking for the correct type with IsKIndOf()).
-
January 15th, 2004, 04:22 AM
#5
Thnx, that worked fine for me
But unfortunately in a more complex situation I created, this resulted in some circular references that I could not solve. Therefor I worked around this problem by making m_data static. There should only be one instance of it anyway so for this particular situation it's the best solution.
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
|