Multiple levels of implementation and implementation inheritance 
Author Message
 Multiple levels of implementation and implementation inheritance

I started out with a typical ATL templated inheritanced structure. My
inheritance graph looks like:

IDispatch
IBase
IDerived and CBaseImpl
IBaseImpl <IDerived>  
IDispatchImpl <IBaseImpl <IDerived> >
CDerived

That is, IBaseImpl <Base> derives from Base and CBaseImpl. IBaseImpl
forwards all its functions to CBaseImpl.

I have a bunch of objects that use the same structure, and use the
same IBase interface and CBaseImpl implementation. CBaseImpl provides
not only the implementation of the IBase methods, but also provides a
bunch of useful functions that are common, and are used in the various
derived classes. Pretty standard stuff, I think.

Now I need to implement a few more objects, that share a common
"middle" interface and implementation. The interface derivations look
like:

IDispatch
IBase
IMiddle
IDerived

The IMiddle interface will have a templated IMiddleImpl<>, and a
common implementation CMiddleImpl. I would like CMiddleImpl to be able
to use the common protected functions in CBaseImpl.

So, the inheritance graph I think I want is:

IDispatch
IBase
IMiddle
             CBaseImpl
IDerived and CMiddleImpl
IBaseImpl <IDerived>  
IDispatchImpl <IBaseImpl <IDerived> >
CDerived

But this doesn't work out, as my "standard" implementation of
IBaseImpl<IDerived> derives from CBaseImpl, not CMiddleImpl.

My quick solution to this, I'm somewhat embarrased to say, is to add a
template parameter to IBaseImpl, so it looks like:

template<typename Base, typename BaseImpl>
class IBaseImpl :
  public Base,
  public BaseImpl

Then when instantiating the original classes, I invoice
  IDispatchImpl <IBaseImpl <IDerived, CBaseImpl> >

When instantiating my new multiple level classes, I invoke
  IDispatchImpl <IBaseImpl <IDerived, CDummyClass> >

where CDummyClass is a dummy class that has no functions or memeber
variables. Then I make CMiddleImpl derive from CBaseImpl. This works
fine, actually, but seems like such a kludge. There must be a better
way.

I tried messing around with other derivation structures, such as
invoking this for my mutiple level classes:
  IDispatchImpl <IBaseImpl <IDerived, CMiddleImpl>

but then CMiddleImpl doesn't derive from CBaseImpl, so can't access
it's member functions and variables.

Any suggestions? What am I missing? TIA,

--
Chris



Tue, 23 Sep 2003 21:15:06 GMT  
 Multiple levels of implementation and implementation inheritance
You can use virtual base class:

class CBaseImpl
{
    int baseData;
public:
    int getData();

Quote:
}

class IBaseImpl : virtual public CBaseImpl {};

class CMiddleImpl : virtual public CBaseImpl {};

This way, when you later use

class CClass : public IBaseImpl, public CMiddleImpl {};

CClass objects will have only one shared subobject of CBaseImpl class and
correspondingly one instance of of baseData member.
--
With best wishes,
    Igor Tandetnik


Quote:
> I started out with a typical ATL templated inheritanced structure. My
> inheritance graph looks like:

> IDispatch
> IBase
> IDerived and CBaseImpl
> IBaseImpl <IDerived>
> IDispatchImpl <IBaseImpl <IDerived> >
> CDerived

> That is, IBaseImpl <Base> derives from Base and CBaseImpl. IBaseImpl
> forwards all its functions to CBaseImpl.

> I have a bunch of objects that use the same structure, and use the
> same IBase interface and CBaseImpl implementation. CBaseImpl provides
> not only the implementation of the IBase methods, but also provides a
> bunch of useful functions that are common, and are used in the various
> derived classes. Pretty standard stuff, I think.

> Now I need to implement a few more objects, that share a common
> "middle" interface and implementation. The interface derivations look
> like:

> IDispatch
> IBase
> IMiddle
> IDerived

> The IMiddle interface will have a templated IMiddleImpl<>, and a
> common implementation CMiddleImpl. I would like CMiddleImpl to be able
> to use the common protected functions in CBaseImpl.

> So, the inheritance graph I think I want is:

> IDispatch
> IBase
> IMiddle
>              CBaseImpl
> IDerived and CMiddleImpl
> IBaseImpl <IDerived>
> IDispatchImpl <IBaseImpl <IDerived> >
> CDerived

> But this doesn't work out, as my "standard" implementation of
> IBaseImpl<IDerived> derives from CBaseImpl, not CMiddleImpl.

> My quick solution to this, I'm somewhat embarrased to say, is to add a
> template parameter to IBaseImpl, so it looks like:

> template<typename Base, typename BaseImpl>
> class IBaseImpl :
>   public Base,
>   public BaseImpl

> Then when instantiating the original classes, I invoice
>   IDispatchImpl <IBaseImpl <IDerived, CBaseImpl> >

> When instantiating my new multiple level classes, I invoke
>   IDispatchImpl <IBaseImpl <IDerived, CDummyClass> >

> where CDummyClass is a dummy class that has no functions or memeber
> variables. Then I make CMiddleImpl derive from CBaseImpl. This works
> fine, actually, but seems like such a kludge. There must be a better
> way.

> I tried messing around with other derivation structures, such as
> invoking this for my mutiple level classes:
>   IDispatchImpl <IBaseImpl <IDerived, CMiddleImpl>

> but then CMiddleImpl doesn't derive from CBaseImpl, so can't access
> it's member functions and variables.

> Any suggestions? What am I missing? TIA,

> --
> Chris



Tue, 23 Sep 2003 22:46:25 GMT  
 Multiple levels of implementation and implementation inheritance
Doh! Of course. Here I am trying to solve this with weird template
mechanisms, and it's just regular old multiple-inheritance.

Thanks!

--
Chris

On Fri, 6 Apr 2001 10:46:25 -0400, "Igor Tandetnik"

Quote:

>You can use virtual base class:

>class CBaseImpl
>{
>    int baseData;
>public:
>    int getData();
>}

>class IBaseImpl : virtual public CBaseImpl {};

>class CMiddleImpl : virtual public CBaseImpl {};

>This way, when you later use

>class CClass : public IBaseImpl, public CMiddleImpl {};

>CClass objects will have only one shared subobject of CBaseImpl class and
>correspondingly one instance of of baseData member.



Wed, 24 Sep 2003 06:19:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Implementation Inheritance w/ ATL 3.0

2. Table-driven QI and implementation inheritance

3. Implementation inheritance

4. Interface & Implementation inheritance question (ignore previous post)

5. Interface & Implementation inheritance question

6. Co-existing Multiple Application within single C implementation

7. Co-existing Multiple Application within single C implementation

8. How to split implementations of interfaces into multiple libs

9. Q: Intf w/multiple implementations

10. Interface w/multiple implementations

11. Interface w/multiple Implementations (VC++)?

12. Multi-Level WinForm Inheritance Problem

 

 
Powered by phpBB® Forum Software