PVD -- Provide Virtual Destructor

From RAD Studio
Jump to: navigation, search

Go Up to C++ Audits

Description

PVD detects classes with virtual methods that have nonvirtual destructors or do not have destructors.

You should declare a destructor virtual whenever the class has at least one virtual function. Having virtual functions indicates that a class is meant to act as an interface to derived classes, and when it is, an object of a derived class may be destroyed through a pointer to the base.

Had the base destructor not been virtual, the derived destructor would not have been called - with likely bad effects, such as resources owned by derived not being freed.

Incorrect

 class Base{
 public:
   virtual method() = 0;
 };

Correct

 class Base{
 public:
   virtual method() = 0;
   virtual ~Base(){};
 };

See Also