I have a kind of "triangle inheritance" problem, if such a thing exists. Basically, I have an abstract base class the defines an interface, and a policy class that defines the implementation to part of that interface, like so:
class System
{
public:
Interface() = default;
virtual ~Interface() {}
virtual void doSomething() = 0;
virtual bool somethingDone() const = 0;
virtual int trait1() const = 0;
virtual bool trait2() const = 0;
};
class SomethingImplementation
{
public:
SomethingImplementation() = default;
void doSomething() { (void)0; }
bool somethingDone() { return true; }
};
And then I have several implementations of the interface for different cases. In some cases, I want to use the simple implementation, and in others I want to customize them
class SystemA : public System, public SomethingImplementation {/*...*/};
class SystemB : public System, public SomethingImplementation {/*...*/};
class SystemC : public System, { /* with a custom implementation ... */ };
What I can't figure out is what the details of class SystemA or SystemB might be to make the implementation work
Solution 1
using statements, don't seem to do anything in this case.
59:13: error: cannot declare variable 'a' to be of abstract type 'SystemA'
22:7: note: because the following virtual functions are pure within 'SystemA':
6:18: note: virtual void System::doSomething()
7:18: note: virtual bool System::somethingDone() const
60:13: error: cannot declare variable 'b' to be of abstract type 'SystemB'
31:7: note: because the following virtual functions are pure within 'SystemB':
6:18: note: virtual void System::doSomething()
7:18: note: virtual bool System::somethingDone() const
63:38: error: invalid new-expression of abstract class type 'SystemA'
Solution 2
Add an extra interface class. Create a SomethingInterface
class and inherit both Interface
and SomethingImplementation
from SomethingInterface
. This turns the problem into a regular "diamond inheritance" problem, but I still can't seem to get the compiler to do what I want.
What is the way to use the implementation from another class for a virtual method of a parent class?