Missing virtual inheritance
A base class is inherited virtually and nonvirtually in the same hierarchy
Description
This defect occurs when:
A class is derived from multiple base classes, and some of those base classes are themselves derived from a common base class.
For instance, a class
Final
is derived from two classes,Intermediate_left
andIntermediate_right
. BothIntermediate_left
andIntermediate_right
are derived from a common class,Base
.At least one of the inheritances from the common base class is
virtual
and at least one is notvirtual
.For instance, the inheritance of
Intermediate_right
fromBase
isvirtual
. The inheritance ofIntermediate_left
fromBase
is notvirtual
.
Risk
If this defect appears, multiple copies of the base class data members appear in the final derived class object. To access the correct copy of the base class data member, you have to qualify the member and method name appropriately in the final derived class. The development is error-prone.
For instance, when the defect occurs, two copies of the base
class data members appear in an object of class Final
.
If you do not qualify method names appropriately in the class Final
,
you can assign a value to a Base
data member but
not retrieve the same value.
You assign the value using a
Base
method accessed throughIntermediate_left
. Therefore, you assign the value to one copy of theBase
member.You retrieve the value using a
Base
method accessed throughIntermediate_right
. Therefore, you retrieve a different copy of theBase
member.
Fix
Declare all the intermediate inheritances
as virtual
when a class is derived from multiple
base classes that are themselves derived from a common base class.
If you indeed want multiple copies of the Base
data
members as represented in the intermediate derived classes, use aggregation
instead of inheritance. For instance, declare two objects of class Intermediate_left
and Intermediate_right
in
the Final
class.
Examples
Result Information
Group: Object oriented |
Language: C++ |
Default: Off |
Command-Line Syntax: MISSING_VIRTUAL_INHERITANCE |
Impact: Medium |
Version History
Introduced in R2015b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)