Base class implementing interface

Hey,
I have a little design issue and i hope you can help me out with this.

I have 3 interfaces defined:

1
2
3
4
5
6
7
8
9
10
11
12
13
class IBase
{
public:
virtual void methodIBase() = 0;
};

class IDerivedA : public IBase
{
};

class IDerivedB : public IBase
{
};


Now if i implement them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Base : public IBase
{
public:
void methodIBase();
};

class DerivedA : public IDerivedA, public Base
{
//methodIBase not implemented by Base
};

class DerivedB : public IDerivedB, public Base
{
//methodIBase not implemented by Base
};


The problem is, that the method implemented by Base isn't recognized to be implemented in the derived classes.
If this is is only 1 method defined in Base, the overhead of implementing the method in the derived classes as well and then call the base function is minimal. But in my IBase class there are a lot of methods and I have many classes deriving from it, so I would have to implement all these methods for every single class then.
I googled this problem and found out that there is no solution for implementing an interface with a Base class.
But is there a better way to do this by design?

Greetings
Last edited on
*Push*
I'm really not figuring out how this could work. Anyone?
This is an example of what's known as the Diamond of Death. Basically, DerivedA inherits twice from IBase, One of those IBase derivations (via Base) implements methodIBase, while the other (via IDerivedA) doesn't.

You can fix this using virtual inheritance. I recommend reading up on multiple inheritance for a more detailed explanation, and to learn how to do this.
Last edited on
As well as MikeyBoy's suggestion you could also take a look at mixin classes.
http://en.wikipedia.org/wiki/Mixin
http://www.drdobbs.com/cpp/mixin-based-programming-in-c/184404445

But i guess you need to think if what you have is truly an inheritance case.
Last edited on
If you used virtual sub-classing, you get rid of the problem, and you get a diamond.

So if you have:
1
2
3
4
class IBase;
class IDerivedA : public IBase;
class Base : public IBase;
class DerivedA : public IDerivedA, public Base;

then IBase is included twice in DerivedA's ancestry. Obviously the compiler will not know which member functions and variables to use, and should complain.

But if you add the virtual keyword there will be only one:
1
2
3
4
class IBase;
class IDerivedA : virtual public IBase;
class Base : virtual public IBase;
class DerivedA : virtual public IDerivedA, virtual public Base;


Exactly how far up the tree you need to go with the 'virtuals' I'm not sure, but a compiler will soon tell you.
'public virtual' and 'virtual public' are identical.

Ref: http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class
Last edited on
Topic archived. No new replies allowed.