In my code I had an interface and its implementation:
class Interface
{
public:
virtual ~Interface() {}
virtual void f() = 0;
};
class Impl : public Interface
{
public:
virtual ~Impl() {}
virtual void f() {
// ...
}
};
However one beautiful day I decided that I need to enrich my interface with a new method, so I created a derived interface:
class InterfaceExt : public Interface
{
public:
virtual ~InterfaceExt() {}
virtual void g() = 0;
};
Now I need an implementation of the new interface, but I would like to reuse the existing implementation of the old interface, so I tried to do something like this:
class ImplExt : public virtual InterfaceExt, public virtual Impl
{
public:
virtual ~ImplExt() {}
virtual void g() {
// ...
}
};
It seems reasonable, compiler should see that all methods from InterfaceExt are accessible in ImplExt. Unfortunately, it doesn't compile:
diamond.cpp:38: error: cannot declare variable 'a' to be of abstract type 'ImplExt'
diamond.cpp:25: note: because the following virtual functions are pure within 'ImplExt':
diamond.cpp:5: note: virtual void Interface::f()
Of course, I can always to something like
virtual void f() {
Impl::f();
}
but in my real code I have a lot of methods, and doing this kind of thing would be quite tedious. I was hoping that there is a better solution.
Thanks for your comments!