MISRA C++:2008 Rule 5-2-3
Casts from a base class to a derived class should not be performed on polymorphic types
Description
Rule Definition
Casts from a base class to a derived class should not be performed on polymorphic types.1
Rationale
Explicitly casting a polymorphic base class into a derived class bypasses the layers of abstraction in the implementation hierarchy, resulting in increased coupling and dependency between the classes.
When using a polymorphic class, avoid performing explicit downcasts. A pointer to the polymorphic base class does not require explicit casting into a pointer to a derived class.
Polyspace Implementation
Polyspace® raises a violation if a base class is cast to a derived class.
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: Expressions |
Category: Advisory |
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.