|
-
April 28th, 1999, 05:30 AM
#1
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
-
April 28th, 1999, 08:18 AM
#2
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
-
April 28th, 1999, 08:53 AM
#3
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
-
April 28th, 1999, 09:58 AM
#4
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
-
April 28th, 1999, 10:04 AM
#5
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
-
April 28th, 1999, 10:33 AM
#6
Re: Why is GetDocument() necessary ?
Is m_pDocument a member of Class CView ???.
-
April 28th, 1999, 10:38 AM
#7
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
-
April 28th, 1999, 01:04 PM
#8
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?
-
April 29th, 1999, 11:03 AM
#9
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
-
April 29th, 1999, 01:59 PM
#10
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()
-
April 29th, 1999, 04:16 PM
#11
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
-
April 30th, 1999, 03:46 AM
#12
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
-
April 30th, 1999, 03:48 AM
#13
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
-
April 30th, 1999, 03:50 AM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|