Multiple inheritance design question

J

jeffc

I have the following situation and I'm not sure how to proceed. Currently
we have a class hierarchy that looks like this

class Base

class MiddleLayer : public Base

class Custom : public MiddleLayer

Now we have a third party set of classes that we need to integrate. I
consider this a "mix in" type of situation, where we just need that
behavior, but we can't alter the code (we don't have the source).

class ThirdParty : public Base

So the first idea off the top of my head is

class Custom : public MiddleLayer, public ThirdParty

The problem of course the lack of a virtual base class. What we really want
is

class MiddleLayer : virtual public Base

class ThirdParty : virtual public Base

But then we'd have to change the third party "source" code. I put "source"
in quotes because most people think of source code as the cpp file, while
they do send us the hpp header files (of course), and technically we could
change them. But I don't think that would be a very good idea. Any ideas
here? By the way, this is the first time this particular third party code
has been used in an actual production environment, so they might not have
thought fully through the problem, I'm not sure.
 
V

Victor Bazarov

jeffc said:
I have the following situation and I'm not sure how to proceed. Currently
we have a class hierarchy that looks like this

class Base

class MiddleLayer : public Base

class Custom : public MiddleLayer

Now we have a third party set of classes that we need to integrate. I
consider this a "mix in" type of situation, where we just need that
behavior, but we can't alter the code (we don't have the source).

class ThirdParty : public Base

So the first idea off the top of my head is

class Custom : public MiddleLayer, public ThirdParty

The problem of course the lack of a virtual base class. What we really want
is

class MiddleLayer : virtual public Base

class ThirdParty : virtual public Base

But then we'd have to change the third party "source" code. I put "source"
in quotes because most people think of source code as the cpp file, while
they do send us the hpp header files (of course), and technically we could
change them. But I don't think that would be a very good idea. Any ideas
here? By the way, this is the first time this particular third party code
has been used in an actual production environment, so they might not have
thought fully through the problem, I'm not sure.

Without knowing more detail on what 'MiddleLayer' does and what the
ThirdParty class is for and why they need to be mixed in, I can only
suggest:

class Custom : public MiddleLayer {
ThirdParty m_thirdparty;
...
};

or

class Custom : public ThirdParty {
MiddleLayer m_middlelayer;
...
};

Yes, you will get two 'Base' objects, but one will be inherited while
the other hidden inside the data member.

You will need to delegate all the functionality from the non-inherited
class, of course.

Victor
 
J

jeffc

Victor Bazarov said:
Without knowing more detail on what 'MiddleLayer' does....

I don't think it's relevant, but I just included it in case it posed some
problem I hadn't thought of.
class Custom : public MiddleLayer {
ThirdParty m_thirdparty;
...
};

That sounds like a possibility - I'll think more about it. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top