CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2004
    Location
    Netherlands
    Posts
    24

    Question 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.

  2. #2
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338
    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();

  3. #3
    Join Date
    Jan 2004
    Location
    Netherlands
    Posts
    24
    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?

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    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()).

  5. #5
    Join Date
    Jan 2004
    Location
    Netherlands
    Posts
    24
    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
  •  





Click Here to Expand Forum to Full Width

Featured