Qt-Autosar-A12.4.1¶
Destructor of a base class shall be public virtual, public override or protected non-virtual
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
If an object is supposed to be destroyed through a pointer or reference to its base class, the destructor in the base class needs to be virtual. Otherwise, destructors for derived types will not be invoked.
Note that if it is prohibited to destroy an object through a pointer or reference to its base class, the destructor in the base class is supposed to be protected.
Example
// $Id: A12-4-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
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
{
};
void F1(A* obj1, C* obj2)
{
// ...
delete obj1; // Only destructor of class A will be invoked
delete obj2; // Both destructors of D and C will be invoked
}
void F2()
{
A* a = new B;
C* c = new D;
F1(a, c);
}
See Also
- JSF December 2005 [8]: AV Rule 78 All base classes with a virtual function shall define a virtual destructor.
- HIC++ v4.0 [9]: 12.2.1 Declare virtual, private or protected the destructor of a type used as a base class.
- SEI CERT C++ Coding Standard [10]: OOP52-CPP: Do not delete a polymorphic object without a virtual destructor.
- C++ Core Guidelines [11]: C.35: A base class destructor should be either public and virtual, or protected and nonvirtual.
- C++ Core Guidelines [11]: Discussion: Make base class destructors public and virtual, or protected and nonvirtual.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
base_class_destructor |
Destructor of a base class shall be public virtual, public override or protected non-virtual. |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allowed_inheriting_classes¶
allowed_inheriting_classes
A set of full qualified type names that are ignored when determining whether a class is a base class or not. This is mainly a workaround for some libc++ implementation details: The standard library uses private inheritance from empty base classes to implement EBO (Empty Base Optimization) for some of its data structures.Type: set[bauhaus.analysis.config.QualifiedName]
Default:
{'std::_Compressed_pair', 'std::__compressed_pair_elem', 'std::__detail::_Hashtable_ebo_helper', 'std::__libcpp_compressed_pair_imp', 'std::__map_value_compare', 'std::__unordered_map_hasher'}