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.
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.