CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2002
    Location
    Vienna, Austria
    Posts
    47

    Question 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

  2. #2
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338
    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.

  3. #3
    Join Date
    Jun 2002
    Location
    Vienna, Austria
    Posts
    47
    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" ?

  4. #4
    Join Date
    Oct 2000
    Location
    India
    Posts
    489
    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

  5. #5
    Join Date
    Jun 2002
    Location
    Vienna, Austria
    Posts
    47
    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.

  6. #6
    Join Date
    Jun 2002
    Location
    Vienna, Austria
    Posts
    47
    may templates help ?

  7. #7
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    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.

  8. #8
    Join Date
    Sep 2002
    Location
    ohio
    Posts
    37
    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.

  9. #9
    Join Date
    Sep 2002
    Location
    ohio
    Posts
    37
    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
  •  





Click Here to Expand Forum to Full Width

Featured