Main Content

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

Since R2020b

Description

Rule Definition

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

Rationale

If a derived class uses all the base class constructors and does not explicitly initialize any additional data member that is not in the base class, reimplementing these constructors in the derived class adds unnecessary lines of code. The reimplementation might not exactly match the constructors in the base class due to human error and might introduce bugs in your code. Moreover, every time you change the base class constructors, you must also update the derived class constructors. This increases the overhead of code maintenance.

In such situations, using inheriting constructors in the derived class solves these issues.

Polyspace Implementation

Polyspace flags a class for violation of this rule if the class satisfies all of these conditions:

  • The class derives from a single base class.

  • The class uses all the base class constructors and reimplements them in the class definition.

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>

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

The reimplementation of B(std::int32_t x, std::int32_t y) and explicit B(std::int32_t x) violates the rule because they are identical to the base class constructors A(std::int32_t x, std::int32_t y) and explicit A(std::int32_t x).

Check Information

Group: Special member functions
Category: Required, Automated

Version History

Introduced in R2020b