-
September 26th, 2002, 04:53 AM
#1
Multiple inheritance
Hallo !
I have some views which handle partly the same commands (e.g. every view has a "OnEditCopy") in the same way.
Thus I want to share that code in a common base class, but my views derive from different base-classes (CRichEditView, CEditView, CTextView...).
Normally it's impossible with MFC to have two base-classes which both derive from CWnd
=> Does anybody know a solution for this ??
Thanks,
CORCOR
-
September 26th, 2002, 04:59 AM
#2
Multiple inheritance is always
Why not using a class "between" CView and your classes?
CView
|
|
CMyViewBase (with the OnEditCopy-Command-Handlers)
|
|
CMySpezialView1 (with the spezial Command-Handlers or with OnEditCopy, if this class handles this event on another way then CMyViewBase)
Another idea would be using macros.
-
September 26th, 2002, 05:09 AM
#3
I can't use a class "between", because not all my views derive from CView.
E.g. one derives from CRichEditView (which derives from CView)
So where would be "between" ?
-
September 26th, 2002, 05:37 AM
#4
Since you are saying that the code is essential the same,
Why dont you simply put it inside the document class or
some other class and make the function a friend to the
view class ?
when ever you need to exec the common code(fn) you could
pass a pointer the view on which to act and voila
your problem is sovled !
CDocument::CommonCode(View*pView) ;
class View1 : ..
{
friend CDocument::CCommonCode(pView* pView) ;
..
..
};
..
..
Regards,
Prem
-
September 26th, 2002, 06:35 AM
#5
Yeah, I also can put it in a "not - CWnd derived class" and derive my views from this class and the old base class, like
class CMyViewBase
{
void DoEditCopy();
}
class CMySpezialView1 : public CScrollView, public CMyViewBase
{
afx_msg void OnEditCopy();
}
void CMySpezialView1::OnEditCopy()
{
DoEditCopy();
}
but then, I need for EACH view and EACH command
*) ON_COMMAND(...)
*) ON_UPDATE_COMMAND_UI(...)
*) OnEditCopy
*) OnUpdateEditCopy
Every time I add a new command, I have to go through all views to attach the command / update-command handlers and maybe forget on view....
So my goal is to modify one class and automatically have it in all views.
-
September 27th, 2002, 12:57 AM
#6
-
September 27th, 2002, 03:28 AM
#7
Why don't u use CFormView where u can place richedit control and edit control and static text control?
U can control which control is visible and enabled, and and 'stretch' the whatever richedit control to fill the whole client area.
FYI CFormView is like a dialog where u can place controls in it.
-
September 27th, 2002, 08:09 AM
#8
Originally posted by corcor
Yeah, I also can put it in a "not - CWnd derived class" and derive my views from this class and the old base class, like
class CMyViewBase
{
void DoEditCopy();
}
class CMySpezialView1 : public CScrollView, public CMyViewBase
{
afx_msg void OnEditCopy();
}
void CMySpezialView1::OnEditCopy()
{
DoEditCopy();
}
but then, I need for EACH view and EACH command
*) ON_COMMAND(...)
*) ON_UPDATE_COMMAND_UI(...)
*) OnEditCopy
*) OnUpdateEditCopy
Every time I add a new command, I have to go through all views to attach the command / update-command handlers and maybe forget on view....
So my goal is to modify one class and automatically have it in all views.
If the implementation for each method will be the same or very close, why not use a shim class to avoid all the rewriting?
template <deriv,base>
class shim : public base
{
protected:
virtual ON_COMMAND(...)
virtual ON_UPDATE_COMMAND_UI(...)
virtual OnEditCopy
virtual OnUpdateEditCopy
}
then you could declare each view like this:
class CNewView : public shim <CNewView,cMyViewBase>
{
//any extra methods can go here
};
class CAnotherNewView : public shim <CAnotherNewView,cMyRichEditViewBase>
{
//any extra methods can go here
};
Last edited by MushroomStamp; September 27th, 2002 at 08:13 AM.
-
September 27th, 2002, 01:18 PM
#9
In doing that most all implementation details can be held in the shim, and you can override where you need something different.
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
|