Why is this an issue?
This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 13.1.1
Category: Advisory
Analysis Type: Decidable,Single Translation Unit
Rationale
Virtual inheritance of base classes is not recommended as it introduces a number of potentially confusing behaviours, such as call by dominance in
diamond hierarchies and changes to the order of initialization of bases.
Example
struct A
{
virtual int32_t foo() { return 1; }
};
struct B : public virtual A // Non-compliant
{
int32_t goo()
{
return foo();
}
};
struct C : public virtual A // Non-compliant
{
int32_t foo() override { return 2; }
};
struct D : C, B
{
};
int main()
{
D d;
return d.goo(); // Calls C::foo(), which may not be expected
}
Copyright The MISRA Consortium Limited © 2023
Resources
Related rules
- S1011 targets the same defect as this rule but for a non-mission-critical context.
- M23_087: MISRA C++ 2023 Rule 8.2.1 - A virtual base class shall only be cast to a derived class by means of
dynamic_cast
- MISRA C++ 2023 Rule 13.1.2 - An accessible base class shall not be both virtual and non-virtual in the same hierarchy
- MISRA C++ 2023 Rule 15.1.1 - An object’s dynamic type shall not be used from within its constructor or destructor