I need to use C++ interface classes to implement an unmanaged DLL in C++. Suppose I have this class structure:
class IA
{
public:
virtual void Foo() = 0;
...
};
class A : public IA
{
public:
virtual void Foo() override { ... }
...
};
class IB
{
public:
virtual void Bar() = 0;
...
};
class B : public IB
, public A
{
public:
virtual void Bar() { ... }
...
};
Now, if I have a pointer to the interface IB
, this would not compile:
IB* b = ...;
b->Foo();
To make it work, I'd have to make IB
inherit from IA
, as such:
class IB : public IA
{
...
};
But then this also wouldn't compile, because now B
is no longer a concrete class, and the compiler expects it to implement IA::Foo
, even though it inherits from A
.
You can fix this using virtual inheritance:
class A : public virtual IA { ... };
class IB : public virtual IA { ... };
class B : public virtual IB, public A { ... };
Which generates a new warning in VC++:
warning C4250: 'B' : inherits 'A::A::Foo' via dominance
As I understand it, this is because now there is more than one declaration of Foo
.
How do I inherit interfaces and their concrete implementation correctly without these issues?