Multiple virtual inheritance

I found the below code in our code base at work, and I think it looks strange. The class RetainCounting is used for implementing reference counting.

1.Should the IProvider really derive from RetainCounting? Isn´t it better if it was just an abstract base class and then let the class that implements IProvider i.e. ProviderImpl derive from the “RetainCounting” instead (i.e. if it needs retain/release features)?

2.If we believe that IProvider should derive from RetainCounting, why do ProviderImpl needs to derive from “RetainCounting” as well, when the IProvider already has this inheritance?

class RetainCounting
{
public:
   virtual ~RetainCounting();
   void retain();
   void release();

private:
   // more code....
};

class IProvider : public virtual RetainCounting
{
public:
   virtual void doSomething() = 0;
};

class ProviderImpl : virtual private IProvider,
                     virtual public RetainCounting
{
public:
   void doSomething() override;
};


Source: oop

Leave a Reply