MISRA C++:2008 Rule 5-2-2
A pointer to a virtual base class shall only be cast to a pointer to a derived class by means of dynamic_cast
説明
ルール定義
A pointer to a virtual base class shall only be cast to a pointer to a derived class by means of dynamic_cast.  1
        
根拠
virtual 基底は、そこから複数のクラスが派生する可能性があることを意味します。
class Base{};
class childA: virtual public Base{};
class childB: virtual public Base{};
class childFinal: public childA, public childB{};;childA と childB がクラス Base の同じインスタンスを共有しています。Base* から childA* または childB* にキャストする場合、コンパイル時には必要なポインター演算が不明です。Base を基準にした子クラスのオフセットは実行時にのみ既知になります。バーチャル基底クラスへのポインターを子クラスへのポインターにダウンキャストする場合は、dynamic_cast を使用します。この目的で他のキャスト操作を使用した場合は、未定義の動作が発生します。
Polyspace 実装
Polyspace® は、以下の条件を満たしている場合に違反を報告します。
- バーチャル基底クラスがダウンキャストされている。 
- キャスト操作が - dynamic_castを使用して行われていない。
トラブルシューティング
ルール違反が想定されるものの、Polyspace から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。
例
チェック情報
| グループ: Expressions | 
| カテゴリ: 必要 | 
バージョン履歴
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.