CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Posts
    29

    Sharing Class Instance between Views in SDI environment ?

    This may be a simple one : I created a multi view SDI environment with a custom class. Since Instances of this class need tobe accessible from within each view I declared them in CDoc (regular declaration 'CSkills m_Skills;') but cannot access any of the member function from the views. Funny enough when I start typing 'm_Skills.' the correct list of member functions is displayed but compiling it creates an error message stating that 'm_Skills' is an undeclared identifier.

    Should place the declartion somewhere else ? I tried going back to CMainFrm causing a number of Link errors (duplicate declaration of m_Skills in object CDoc ?!?!).

    Thank you for your suggestions.

    Pras


  2. #2
    Join Date
    Apr 1999
    Location
    SungNam KyungKi Korea
    Posts
    14

    Re: Sharing Class Instance between Views in SDI environment ?

    maybe , I think
    You shoud include your header file in order.
    for example, You need to include your header file 'myview.h' for CMyView Class. But if CMyView Class has a member-function called GetDocument(),
    you should also include a header-file 'mydoc.h' for CMyDoc class .
    So,
    #include "mydoc.h"
    #include "myview.h"

    never do this
    #include "myview.h"
    #include "mydoc.h"

    Is that your answer? or I am very sorry.


    by nfuox

  3. #3
    Join Date
    Apr 1999
    Posts
    383

    Re: Sharing Class Instance between Views in SDI environment ?

    To directly access the member variables of another class instance (e.g. your document), they must be declared public in that class.

    From the view, you can then access them by casting the return value of GetDocument() to your own derived document type:

    #include "MyDocument.h"
    ...
    MyDocument* pMyDoc = dynamic_cast<MyDocument*>(GetDocument());
    CSkills localSkills = pMyDoc->m_Skills;

    It may be simpler to change the GetDocument() function in your derived view to return a pointer to your derived document:

    MyDocument* MyView::GetDocument()
    {
    return dynamic_cast<MyDocument*>(CView::GetDocument());
    }

    Be aware that there is an inline release mode version of GetDocument() in the view header file, as well as the out of line debug version.

    Dave


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