Skip to main content

IAR Embedded Workbench for RX 5.20

MISRAC++2023-8.2.1 (C++ only)

In this section:
Synopsis

(Required) A virtual base class shall only be cast to a derived class by means of dynamic_cast

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

Use dynamic_cast to cast an object between a virtual base class and a derived class This check is identical to MISRAC++2008-5-2-2.

Coding standards
MISRA C++ 2008 5-2-2

(Required) A pointer to a virtual base class shall only be cast to a pointer to a derived class by means of dynamic_cast.

Code examples

The following code example fails the check and will give a warning:

class Base { public: virtual ~Base() {} };
class Derived: public virtual Base { public: Derived() {} };

void example(Base& b) {
  auto& d  = reinterpret_cast<Derived&>(b); // Non-compliant
}

The following code example passes the check and will not give a warning about this issue:

class Base { public: virtual ~Base() {} };
class Derived: public virtual Base { public: Derived() {} };

void example(Base& b) {
  auto& d  = dynamic_cast<Derived&>(b); // Compliant
}