Multiple doc templates in a SDI
Dan Green -- s3036076@pompeii.mpce.mq.edu.au
Monday, September 02, 1996
Environment: VC++ 4.0 / NT 3.51
I have a SDI app using CFormView as the view type. It has a
corresponding CDocument derived class (CFooDoc).
To add a twist, I require an extra document (CIndexDoc) that will handle
an index relating to the information contained in CFooDoc.
Without going into details, trust me when I say that CIndexDoc must be
self-contained and serialized to a DIFFERENT file than CFooDoc.
OK, to facilitate this I added a public CSingleDocTemplate* member to
the CWinApp derived class (CFooApp) and instantiate it in
CFooApp::InitInstance(). (I purposely do not use AddDocTemplate() as I
don't want the application to maintain it.)
So far so good (I hope).
What has me concerned, is that to reference, for example, the public
function CIndexDoc::Kaboom() I use:
((CFooDoc*)(((CFooApp*)AfxGetApp())->pIndexDoc))->Kaboom();
Sheesh! If that ain't the ugliest thing you've ever seen?!
Please show me the sensible version which MUST exist.
Thanks for wading through the swamp with me,
Dan.
---
Daniel Green
s3036076@pompeii.mpce.mq.edu.au
Roger Onslow -- Roger_Onslow@compsys.com.au
Tuesday, September 03, 1996
[Mini-digest: 2 responses]
> ((CFooDoc*)(((CFooApp*)AfxGetApp())->pIndexDoc))->Kaboom();
>Sheesh! If that ain't the ugliest thing you've ever seen?!
Sure is -- mind you, you haven't seen me first thing in the morning :-)
Lets break it into bits:
((CFooApp*)AfxGetApp())
yuk, lets define a static member function of your app like this
class CFooApp : public CWinApp {
...
static CFooApp* TheApp () { return static_cast(::AfxGetApp()); }
...
};
NOTE: I used C++ static_cast operator because these new operators help document
why you are casting and help avoid mis-casts -- if you are using an easrlier
version of VC++, just use an old-fashioned cast.
OK, now lets take care of getting at the index doc
class CFooApp : public CWinApp {
...
CFooDoc* GetIndexDoc() { return pIndexDoc; }
...
};
NOTE: I would have used m_pIndexDoc as per MFC conventions, but that's just me
(and MS!) :-)
Of course, you would then make pIndexDoc private -- shouldn't flash your
internal bits to the whole world!!
Now you can do
(CFooApp::TheApp()->GetIndexDoc())->Kaboom()
which is not *quite* so ugly.
Of couse, you could write:
class CFooApp : public CWinApp {
...
void IndexDocKaboom () { GetIndexDoc()->Kaboom(); }
...
};
And then just do:
CFooApp::TheApp()->IndexDocKaboom()
which is almost nice!!
>Thanks for wading through the swamp with me,
What out for the leeches !!
Roger Onslow
RogerO@compsys.com.au
[ Homer: "Operator, Give me the number for 911" -- it's MFC-L :-)]
-----From: David Wruck
Have you considered writing helper functions that perform the casts for =
you? =20
For example, you could write a "CFooApp* GetFooApp()" function that =
calls AfxGetApp() and performs the cast from CWinApp* to CFooApp*. You =
could then add a member function to CFooApp called "CIndexDoc* =
GetIndexDoc()" that returns the pointer to your document.
Calls would then look like this: GetFooApp()->GetIndexDoc()->Kaboom();
To improve on this, you can even dereference the pointers to return a =
reference instead. This will allow you to use "." notation instead of =
pointer "->" notation.
I hope this helps.
rkumar@mail.cswl.com
Thursday, September 05, 1996
Environment: VC++ 4.0 / NT 3.51
I have a SDI app using CFormView as the view type. It has a
corresponding CDocument derived class (CFooDoc).
To add a twist, I require an extra document (CIndexDoc) that will handle
an index relating to the information contained in CFooDoc.
Without going into details, trust me when I say that CIndexDoc must be
self-contained and serialized to a DIFFERENT file than CFooDoc.
OK, to facilitate this I added a public CSingleDocTemplate* member to
the CWinApp derived class (CFooApp) and instantiate it in
CFooApp::InitInstance(). (I purposely do not use AddDocTemplate() as I
don't want the application to maintain it.)
So far so good (I hope).
What has me concerned, is that to reference, for example, the public
function CIndexDoc::Kaboom() I use:
((CFooDoc*)(((CFooApp*)AfxGetApp())->pIndexDoc))->Kaboom();
Sheesh! If that ain't the ugliest thing you've ever seen?!
Please show me the sensible version which MUST exist.
Thanks for wading through the swamp with me,
Dan.
---
Daniel Green
s3036076@pompeii.mpce.mq.edu.au
Other way to have nice thing do the following:-
Add a public member function say GetIndexDocument() to your application class,
which will return pointer to IndexDoc. This is other (sensible) way of doing
what U have done.
Look like this.,
CFooDoc* CFooApp::GetIndexDocument()
{
return ((CFooApp*)AfxGetApp())->pIndexDoc;
}
| Вернуться в корень Архива
|