Interface hierarchy emulation

L

Lorenzo Castelli

Hi,

I'm trying to emulate the behavior of Java and C# interfaces in C++.
In particular I want to create a hierarchy among the interfaces and a
corresponding hierarchy among the implementations.

I tried to implement it using abstract base classes and multiple
inheritance, but I'm having some problems.

Here is some sample code:

class BaseInterface
{
public:
virtual void Foo() = 0;
};

class DerivedInterface :
public BaseInterface
{
public:
virtual void Bar() = 0;
};

class BaseImplementation :
public BaseInterface
{
public:
void Foo() { }
};

class DerivedImplementation :
public BaseImplementation,
public DerivedInterface
{
public:
void Bar() { }
};

int main()
{
BaseImplementation baseImpl;
baseImpl.Foo();

DerivedImplementation derImpl;
derImpl.Foo();
derImpl.Bar();
}


This doesn't compile (VC++ 2003 and G++) because DerivedImplementation
doesn't define void BaseInterface::Foo().
That method is in fact defined by BaseImplementation: with
DerivedImplementation I just want to extend the functionalities of
BaseImplementation to cover the methods declared in DerivedInterface.

If I change DerivedInterface and BaseImplementation to use virtual base
classes, it compiles and works:

class DerivedInterface :
public virtual BaseInterface
{
public:
virtual void Bar() = 0;
};

class BaseImplementation :
public virtual BaseInterface
{
public:
void Foo() { }
};

VC++ gives a warning message though ('DerivedImplementation' : inherits
'BaseImplementation::BaseImplementation::Foo' via dominance).
Moreover this is not exactly what I want to do: I don't care about
duplication of BaseInterface in DerivedImplementation because BaseInterface
has only pure virtual methods.

What I want to achieve can be implemented explicitly defining
DerivedImplementation in this way:

class DerivedImplementation :
public BaseImplementation,
public DerivedInterface
{
public:
void Foo() { BaseImplementation::Foo(); }
void Bar() { }
};

This works, but redirecting every method to the base class seems a little
excessive to me.


Do you have any suggestion?


Thanks.
 
B

benben

Lorenzo Castelli said:
Hi,

I'm trying to emulate the behavior of Java and C# interfaces in C++.
In particular I want to create a hierarchy among the interfaces and a
corresponding hierarchy among the implementations.

I tried to implement it using abstract base classes and multiple
inheritance, but I'm having some problems.

Here is some sample code:

class BaseInterface
{
public:
virtual void Foo() = 0;
};

class DerivedInterface :
public BaseInterface
{
public:
virtual void Bar() = 0;
};

class BaseImplementation :
public BaseInterface
{
public:
void Foo() { }
};

class DerivedImplementation :
public BaseImplementation,
public DerivedInterface
{
public:
void Bar() { }
};

Try this (I'm not quite sure if its gonna work, don't current have a
compiler with me...)

class DerivedImplementation:
public BaseImplementation,
public DerivedInterface
{
public:
void Foo(){BaseImplemenation::Foo();}
void Bar(){}
};

OR

class DerivedImplementation:
public BaseImplementation,
public DerivedInterface
{
public:
using BaseImplementation::Foo;

void Bar(){}
};

Compile them each at a time and come back and tell me which works :)

Ben
 
L

Lorenzo Castelli

benben said:
Try this (I'm not quite sure if its gonna work, don't current have a
compiler with me...)

class DerivedImplementation:
public BaseImplementation,
public DerivedInterface
{
public:
void Foo(){BaseImplemenation::Foo();}
void Bar(){}
};

OR

class DerivedImplementation:
public BaseImplementation,
public DerivedInterface
{
public:
using BaseImplementation::Foo;

void Bar(){}
};

Compile them each at a time and come back and tell me which works :)

The first one works, but I already tried this (see the end of my previous
post).
If possible I'd like to avoid to redirect all the BaseInterface methods to
BaseImplementation.

The second one doesn't work.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top