Specify which base class function should be overriden in case of "triangle like" multiple inheritance pattern

80 Views Asked by At

I have an abstract class used by objects that want to use some fooization mechanism

#include <unordered_set>

class Fooable
{
public:
    Fooable() { registered.insert(this); }
    ~Fooable() { registered.erase(this); }
    virtual void fooify() = 0;

    static void fooify_all()
    {
        for(auto * f : registered)
            f->fooify();
    }

private:
    static std::unordered_set<Fooable*> registered;
};

std::unordered_set<Fooable*> Fooable::registered;

Then a base class (note the private inheritance):

class Fruit : private Fooable
{
    void fooify() override final { //Access class private members }
};

And a derived class

class Apple : public Fruit, private Fooable
{
    void fooify() override final { //Access class private members }
};

Both Apple and Fruit want to have their own private definition of fooify, which uses the class private variables and do stuff. And at the end fooify will be called for both class implementations, independently of each other.

The issue is, the compiler complains when Apple tries to override a virtual function marked with final. While those functions should be unrelated.

I could add levels of indirection, but this would make the code use lots of accessors or friend classes, as fooify does access a lot of private member variables.

Any idea to make this work or redesign it a nice way?

Example that fails to compile because of final: https://onlinegdb.com/CbcHXryX2

Example without final that compiles but shows Apple overrides indirectly inherited Fruit::fooify while I would like it to implement its directly inherited Fooable::fooify : https://onlinegdb.com/xH-MNxpVW

0

There are 0 best solutions below