Say I have the following interface class in order to "Write to interfaces, not implementations":
class IDrawable
{
public:
virtual void
Draw() const = 0;
protected:
~IDrawable() = default;
};
Clients should not be able to delete dynamically allocated drawables trough a interface pointer so IDrawable's destructor is made protected and non-virtual, as per C++ Core guideline C.35: A base class destructor should be either public and virtual, or protected and nonvirtual.
Now for a class that implements this interface:
class CDrawable : public IDrawable
{
public:
void
Draw() const override;
};
This, off course, emits a warning:
CDrawablehas virtual functions but non-virtual destructor.
To solve this we now have to add a virtual destructor to CDrawable. But it feels like a code smell to have a virtual destructor in a derived class, I've not seen that before (?) Wouldn't it make more sense to have a virtual protected destructor in IDrawable, contrary to what the guideline says?