Click to See Complete Forum and Search --> : Why is GetDocument() necessary ?


Brendan Cullen
April 28th, 1999, 05:30 AM
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

Braulio
April 28th, 1999, 08:18 AM
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

Brendan Cullen
April 28th, 1999, 08:53 AM
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

PKTonder
April 28th, 1999, 09:58 AM
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

Brendan Cullen
April 28th, 1999, 10:04 AM
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

Andy St.Clair
April 28th, 1999, 10:33 AM
Is m_pDocument a member of Class CView ???.

Brendan Cullen
April 28th, 1999, 10:38 AM
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

Andy St.Clair
April 28th, 1999, 01:04 PM
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?

Brendan Cullen
April 29th, 1999, 11:03 AM
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

April 29th, 1999, 01:59 PM
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()

April 29th, 1999, 04:16 PM
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

Brendan Cullen
April 30th, 1999, 03:46 AM
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

Brendan Cullen
April 30th, 1999, 03:48 AM
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

Brendan Cullen
April 30th, 1999, 03:50 AM
Thanks !

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

Brendan