AUTOSAR C++14 Rule A12-4-1
Destructor of a base class shall be public virtual, public override or protected non-virtual
Since R2020a
Description
Rule Definition
Destructor of a base class shall be public virtual, public override or protected non-virtual.
Rationale
If a base class destructor is not public virtual or public override, the class cannot behave polymorphically for deletion of derived class objects.
If a pointer to a base class refers to a derived class object and you use the pointer to delete the object:
class Base { public: ~Base() {} }; class Derived: public Base { public: ~Derived() {} }; ... void func(Base* ptr) { //ptr might point to a Base or Derived object delete ptr; }
If you want to prevent calling the derived class destructor through a base class pointer, make your intent explicit by making the destructor protected. Otherwise, it might appear that the possibility of polymorphic deletion of derived class objects was not considered.
Polyspace Implementation
The checker flags base classes with destructors that are not public virtual, public override or protected non-virtual.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Special member functions |
Category: Required, Automated |
Version History
Introduced in R2020a