12.2 Destructors

12.2.1 Declare virtual, private or protected the destructor of a type used as a base class

If an object will ever be destroyed through a pointer to its base class, then the destructor in the base class should be virtual. If the base class destructor is not virtual, then the destructors for derived classes will not be invoked. Where an object will not be deleted via a pointer to its base, then the destructor should be declared with protected or private access. This will result in a compile error should an attempt be made to delete the object incorrectly.

#include <cstdint>
   
class A
{
public:
~A ();         // @@- Non-Compliant -@@
};
               
class B : public A
{
};
               
class C
{
public:
virtual ~C (); // @@+ Compliant +@@
};
               
class D : public C
{
};
               
class E
{
protected:
~E ();         // @@+ Compliant +@@
};
               
class F : public E
{
};

References

  • HIC++ v3.3 – 3.3.2

Click here for references