I am trying to resolve a dreaded diamond problem with virtual class methods.
Let us first consider a multiple inheritance case with the peculiarity of a final virtual method. Since there is a final method one cannot declare an override method but has to use a using-declaration to specify which method should be used for the child class.
class Mother {
public:
virtual void foo() final {}
};
class Father {
public:
virtual void foo() {}
};
class Child: public Mother, public Father {
public:
// void foo() {Mother::foo();} // This clashes with Mother::foo being final
using Mother::foo;
};
The above code works as expected.
However, if we switch to a diamond-like structure with an abstract base class the same approach will no longer work.
class GrandParent {
public:
virtual void foo() = 0;
};
class Mother: virtual public GrandParent{
public:
virtual void foo() override final {};
};
class Father: virtual public GrandParent{
public:
virtual void foo() override {};
};
class Child: public Mother, public Father {
using Mother::foo;
};
Compiling the above code will raise the error: no unique final overrider for ‘virtual void GrandParent::foo()’ in ‘Child’
.
Any ideas on how to solve this issue?