メインコンテンツ

AUTOSAR C++14 Rule A11-0-2

A type defined as struct shall:(1) provide only public data members, (2) not provide any special member functions or methods, (3) not be a base of another struct or class, (4) not inherit from another struct or class

説明

ルール定義

A type defined as struct shall:(1) provide only public data members, (2) not provide any special member functions or methods, (3) not be a base of another struct or class, (4) not inherit from another struct or class.

根拠

このルールによって禁止される項目は、C コード内の struct 型でサポートされません。C++ では、struct 型は、プライベート データ メンバーとメンバー関数をもつことができ、他の struct または class から継承されたり、それらを継承したりできます。ただし、開発者の多くは、これらの機能を class 型に関連付けます。このルールに従うことによって、クラスのみを使用してデータのカプセル化や継承などのオブジェクト指向の概念を実装します。

また、このルールに従うことによって、struct 型は、Plain Old Data (POD) 型のルールに従い、C コードと交換できるようになります。

Polyspace 実装

チェッカーは、以下の機能の 1 つ以上を備えた struct 型にフラグを設定します。

  • プライベートまたは保護されたデータ メンバーを含む。

    struct メンバーは既定でパブリックです。

  • メンバー関数を含む。

  • 他の struct または class の基底クラスとして機能するか、他の struct または class から継承する。

トラブルシューティング

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

すべて展開する

#include <cstdint>
#include <iostream>

struct loginCredentials1 { //Noncompliant: Private members
    int32_t username;
private:
    int32_t pwd;   
};

struct loginCredentials2 {   //Noncompliant: Member functions
    int32_t username;
    int32_t pwd; 
    void readFromFile(std::string fileName) {
        //Read members data from file
    }
};

struct loginCredentials3 {  //Noncompliant: Acts as base for another struct
    int32_t username;
    int32_t pwd;
};

struct adminLoginCredentials: loginCredentials3 { //Noncompliant: Inherits from another struct
    std::string permissions;    
};

この例では、すべての struct 型が非準拠です。

  • loginCredentials1 にプライベート データ メンバー pwd が含まれている。

  • loginCredentials2 にメンバー関数 readFromFile() が含まれている。

  • loginCredentials3 が struct adminLoginCredentials の基底として機能する。

  • adminLoginCredentials が struct loginCredentials3 から継承する。

チェック情報

グループ: メンバー アクセス制御
カテゴリ: Required、Automated

バージョン履歴

R2020a で導入