C26436 NEED_VIRTUAL_DTOR

"The type with a virtual function needs either public virtual or protected nonvirtual destructor."

C++ Core Guidelines: C.35: A base class destructor should be either public and virtual, or protected and nonvirtual

If a class defines a virtual function it becomes polymorphic, which implies that derived classes can change its behavior including resource management and destruction logic. Because client code may call polymorphic types via pointers to base classes, there is no way a client can explicitly choose which behavior is appropriate without downcasting. To make sure that resources are managed consistently and destruction occurs according to the actual type’s rules it is recommended to define a public virtual destructor. If the type hierarchy is designed to disallow client code to destroy objects directly, destructors should be defined as protected non-virtual.

Remarks

  • The warning shows up on the first virtual function definition of a type (it can be a virtual destructor if it is not public), once per type.
    • Since definition can be placed separately from declaration, it may not always have any of the virtual specifiers. But the warning is still valid – it checks the actual ‘virtuality’ of a function.