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
Finalis derived from two classes,Intermediate_leftandIntermediate_right. BothIntermediate_leftandIntermediate_rightare derived from a common class,Base.At least one of the inheritances from the common base class is
virtualand at least one is notvirtual.For instance, the inheritance of
Intermediate_rightfromBaseisvirtual. The inheritance ofIntermediate_leftfromBaseis 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
Basemethod accessed throughIntermediate_left. Therefore, you assign the value to one copy of theBasemember.You retrieve the value using a
Basemethod accessed throughIntermediate_right. Therefore, you retrieve a different copy of theBasemember.
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)