Topic: Optional inheritance or classes to use same code ????


Author: jarkko@relatech.fi (Jarkko Muhonen)
Date: Thu, 7 Apr 1994 12:27:38
Raw View
Hello all C++ programmers !

Is there some way to build class with  optional Inheritance (or three classes
to use same code). I have class that I want optionally herit from
CFormView, CScrollView or CDialog in MFC 2.5 ??.  I can't use multiple
inheritance because they all have herited from CWnd !!I don't want to build
three diffirent classes because class is very large.

// ????????????????????????????

class MyWnd : public (CFormView or CScrollView or CDialog) ???????







Author: barmar@think.com (Barry Margolin)
Date: 8 Apr 1994 18:06:20 GMT
Raw View
In article <jarkko.23.000C7646@relatech.fi> jarkko@relatech.fi (Jarkko Muhonen) writes:
>Is there some way to build class with  optional Inheritance (or three classes
>to use same code). I have class that I want optionally herit from
>CFormView, CScrollView or CDialog in MFC 2.5 ??.  I can't use multiple
>inheritance because they all have herited from CWnd !!I don't want to build
>three diffirent classes because class is very large.

If they use virtual inheritance, then you can use multiple inheritance and
there will only be one copy of the CWnd base class in the final object.

Also, classes don't take up much space, only objects do.  So you could
declare three different classes, and then choose which one to instantiate:

class MyWnd: public virtual CWnd { ... }; // abstract base class
class MyCFormView: public MyWnd, CFormView { ... };
class MyCScrollView: public MyWnd, CScrollView { ... };
class MyCDialog: public MyWnd, CDialog { ... };

...

MyWnd *wnd;

switch (window_type) {
case FORM: wnd = new MyCFormView; break;
case SCROLL: wnd = new MyCScrollView; break;
case DIALOG: wnd = new MyCDialog; break;
default: // report error
}

>class MyWnd : public (CFormView or CScrollView or CDialog) ???????

Class declarations are processed at compile time.  How would this option be
processed then?

P.S. This question isn't really appropriate for comp.std.c++ --
comp.lang.c++ would be better.  Unless you want to suggest adding a feature
to the standard that is being designed.
--
Barry Margolin
System Manager, Thinking Machines Corp.

barmar@think.com          {uunet,harvard}!think!barmar