Main Content

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

Since R2020a

Description

Rule Definition

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.

Rationale

The items prohibited by this rule are not supported for struct types in C code. In C++, a struct type can have private data members, member functions, be inherited and inherit from other struct-s or class-es. However, a developer typically associates these features with a class type. Adhering to this rule makes sure that you use only classes to implement object oriented concepts such as data encapsulation and inheritance.

Adhering to this rule also makes sure that your struct types conform to the rules of Plain Old Data (POD) types and can be exchanged with C code.

Polyspace Implementation

The checker flags struct types with one or more of these features:

  • Contains private or protected data members.

    struct members are public by default.

  • Contains member functions.

  • Acts as base class for another struct or class, or inherits from another struct or 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

expand all

#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;    
};

In this example, all struct types are noncompliant.

  • loginCredentials1 contains a private data member pwd.

  • loginCredentials2 contains a member function readFromFile().

  • loginCredentials3 acts as a base for the struct adminLoginCredentials.

  • adminLoginCredentials inherits from the struct loginCredentials3.

Check Information

Group: Member access control
Category: Required, Automated

Version History

Introduced in R2020a