メインコンテンツ

AUTOSAR C++14 Rule A12-1-6

Derived classes that do not need further explicit initialization and require all the constructors from the base class shall use inheriting constructors

説明

ルール定義

Derived classes that do not need further explicit initialization and require all the constructors from the base class shall use inheriting constructors.

根拠

派生クラスが基底クラスのすべてのコンストラクターを使用していて、基底クラスに含まれない他のデータ メンバーを明示的に初期化しない場合、これらのコンストラクターを派生クラスに再実装すると、不要なコード行が追加されます。人的エラーにより、再実装が基底クラスのコンストラクターと完全に一致しなくなる可能性や、コード内のバグにつながる可能性があります。さらに、基底クラス コンストラクターに変更を加えるたびに、派生クラス コンストラクターも更新しなければなりません。これにより、コード保守のオーバーヘッドが増加します。

このような場合、派生クラスで継承コンストラクターを使用すると問題が解決されます。

Polyspace 実装

Polyspace は、以下の条件をすべて満たすクラスには、このルールへの違反としてフラグを設定します。

  • クラスが単一の基底クラスから派生している。

  • クラスが基底クラスのすべてのコンストラクターを使用していて、それらのコンストラクターをクラス定義に再実装している。

トラブルシューティング

ルール違反が想定されるものの、Polyspace® から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。

すべて展開する

#include <cstdint>

class A               
{
  public:
    A(std::int32_t x, std::int32_t y) : x(x + 8), y(y) {}
    explicit A(std::int32_t x) : A(x, 0) {}

  private:
    std::int32_t x;
    std::int32_t y;
};

class B : public A    // Non-compliant
{
  public:
    B(std::int32_t x, std::int32_t y) : A(x, y) {}
    explicit B(std::int32_t x) : A(x) {}
};

class C : public A   // Compliant
{
  public:
    using A::A;
};

B(std::int32_t x, std::int32_t y) および explicit B(std::int32_t x) は基底クラス コンストラクター A(std::int32_t x, std::int32_t y)explicit A(std::int32_t x) と同一であるため、これらを再実装するとルールに違反します。

チェック情報

グループ: 特殊なメンバー関数
カテゴリ: Required、Automated

バージョン履歴

R2020b で導入