CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Why is GetDocument() necessary ?

    Hi folks,

    If we're in a view, and we want access to our associated document, why don't we simply use the m_pDocument data member ? Why do we have GetDocument() ?

    Regards,

    Brendan


  2. #2
    Join Date
    May 1999
    Location
    Spain
    Posts
    335

    Re: Why is GetDocument() necessary ?

    Hi,

    In theory, all the member variables of a class cannot be accessed by another class, need methods to comuniccate with other classes, this is the pretty way, but we have public and friends and...

    But I think that is better to avoid manage with public member variables and such things ( when is possible).

    Sometimes OOP and C++ are not so friends ;-)

    Bye !
    Braulio


  3. #3
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: Why is GetDocument() necessary ?

    Hi Braulio,

    I'm not quite sure what you mean. If I am already inside the view class, why do I call GetDocument() ? I can safely use my own data member - m_pDocument ! That is why I do not know why I see these GetDocument() function calls inside view classes.

    Regards,

    Brendan


  4. #4
    Join Date
    Apr 1999
    Posts
    6

    Re: Why is GetDocument() necessary ?

    It is simple really.The MFC guys have done it to be able to use the same access method in Debug mode as in Release mode. Using GetDocument
    you don't have to use #ifdefs youself, the MFC generated code has done it for you. So to conclude, this is done to make the code more readable.
    Since GetDocument is an inline function it really shouldn't give any overhead either. But go ahead, use m_pDocument if you want to, but then
    no Debug support is provided


  5. #5
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: Why is GetDocument() necessary ?

    Hi Pal,

    >>It is simple really.The MFC guys have done it to be able to use the same access method in Debug mode as in Release mode. Using GetDocument
    you don't have to use #ifdefs youself, the MFC generated code has done it for you. So to conclude, this is done to make the code more readable.
    Since GetDocument is an inline function it really shouldn't give any overhead either. But go ahead, use m_pDocument if you want to, but then
    no Debug support is provided

    Thanks for replying. What do you mean by saying that no debug support is provided if I use m_pDocument ?

    Brendan




  6. #6
    Join Date
    May 1999
    Posts
    25

    Re: Why is GetDocument() necessary ?

    Is m_pDocument a member of Class CView ???.


  7. #7
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: Why is GetDocument() necessary ?

    Hi Andy,

    Yes - m_pDocument is indeed a protected data member of CView. The class definition is in ...mfc\include\afxwin.h (or at least it is for the version 5.0 that I use).

    Regards,

    Brendan


  8. #8
    Join Date
    May 1999
    Posts
    25

    Re: Why is GetDocument() necessary ?

    Yes, it's there also in Version 6.

    I suppose in a class derived from CView then the
    procted m_pDocument value is readily available.
    Other classes wanting to get the m_pDocument value will have to use the public function
    GetDocument() because of the protected status of
    m_pDocument.

    What more can I say?








  9. #9
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: Why is GetDocument() necessary ?

    Hi Pal,

    >>Since GetDocument is an inline function it really shouldn't give any overhead either.

    I am using VC++5.0. In it, in afxwin.h, CView::GetEDocument() is a public member function and is not inline. Is it declared inline in VC++6.0 ?

    Regards,

    Brendan


  10. #10
    Guest

    Re: Why is GetDocument() necessary ?

    There's a simple reason ...

    GetDocument() is defined differently for debug and release build.

    For release build its an inline function (all versions of MFC) with no overhead.

    For debug build there are a few ASSERTs to ensure you're actually referencing a CDocument derived class.

    There are no reasons not to use GetDocument()



  11. #11
    Guest

    Re: Why is GetDocument() necessary ?

    Hi Brendan,

    in debug mode GetDocument is a normal member function in CYourView.cpp. The ASSERT statement guarantees that m_pDocument is really of class CYourDoc. The release version is defined in CYourView.h. In both cases GetDocument does the type cast from CDocument to CYourDoc for you.

    HTH

    Martin


  12. #12
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: Why is GetDocument() necessary ?

    Hi Andy,

    Classes other than the view classes would want to be careful in using GetDocument() because it is not virtual. They may accidentally call CDocument's GetDocument() function rather than their own document class' GetDocument(). Such wishes to get access to the document from outside of views should consider the GetActiveDocument() route.

    Brendan


  13. #13
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: Why is GetDocument() necessary ?

    Hi Martin,

    Yep - that helps. I had mistakenly been looking only at MFC's source code (afxwin.h and appcore.cpp). I had neglected to look at the Appwizard generated code.

    Thanks,

    Brendan


  14. #14
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: Why is GetDocument() necessary ?

    Thanks !

    I had mistakenly only looked at the MFC source code. I had neglected to look at the default AppWizard produced code.

    Brendan


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