MISRA C++:2008 Rule 10-1-2
A base class shall only be declared virtual if it is used in a diamond hierarchy
Description
Rule Definition
A base class shall only be declared virtual if it is used in a diamond hierarchy.1
Rationale
This rule is less restrictive than MISRA C++:2008 Rule 10-1-1
. Rule 10-1-1
forbids the use of a virtual base anywhere in your code because a virtual base can
lead to potentially confusing behavior.
Rule 10-1-2 allows the use of virtual bases in the one situation where they are useful, that is, as a common base class in diamond hierarchies.
For instance, the following diamond hierarchy violates rule 10-1-1 but not rule 10-1-2.
class Base {}; class Intermediate1: public virtual Base {}; class Intermediate2: public virtual Base {}; class Final: public Intermediate1, public Intermediate2 {};
Polyspace Implementation
Polyspace® flags a class if the class is used as a virtual base in a single and linear
hierarchy. In such a hierarchy, virtual
bases are unnecessary.
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: Derived Classes |
Category: Required |
Version History
Introduced in R2013b
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.