Main Content

AUTOSAR C++14 Rule A10-4-1

Hierarchies should be based on interface classes

Since R2021b

Description

Rule Definition

Hierarchies should be based on interface classes.

Rationale

An interface class has these properties:

  • If the class has member functions, they are public and pure virtual.

  • If the class has data members, they are public and static constexpr.

Using an interface class as a base class in a hierarchy:

  • Separates the interface and implementation. The code of the base class is more stable and easier to maintain.

  • Avoids unnecessary computations of nonstatic data members in the derived classes and other compilation dependencies.

  • Makes your software easier to extend and enables the use of alternative implementations through the same interface.

Polyspace Implementation

Polyspace® flags the base of a class hierarchy if that base class is not an interface.

When class definitions are nested in other classes, the checker follows these conventions:

  • Non-interface base classes are flagged even if the hierarchy is nested inside another class. For example, in this code snippet, class NestedBase is flagged :

    class ClassWithNestedHierarchy
    {
        class NestedBase //Non-compliant, not an interface
        {
        public:
            int i;
        };
    
        class NestedDerived : public NestedBase
        {
        public:
            int j;
        };
    };

  • Base classes with nested non-interface classes are not flagged. For example, in this code snippet, NestedClass is not an interface class but the outer class InterfaceWithInnerClass is not flagged when used as a base class:

    class InterfaceWithInnerClass
    {
    public:
        class NestedClass   //not an interface class
        {
        private:
            int i;
        };
    
        static constexpr NestedClass i{};
    };
    
    class DerivedBaseWithInnerClass : public InterfaceWithInnerClass
    {
    private:
        int i;
    };

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

class Interface
{
public:
    virtual ~Interface() = 0;
    virtual void SomeFunc() = 0;
};

class NotAnInterface //Non-compliant
{
public:
    void Implementation() {}

};

class IsDerived1 : public Interface
{

public:
    ~IsDerived1() {}

    void SomeFunc() final {}

};


class IsDerived2 : public NotAnInterface
{
public:
    IsDerived2() = default;
};


template <typename T>
class TmplInterface
{
public:
    virtual T func() noexcept = 0;

};
template<typename T>
class TmplNotInterface //Non-compliant
{
public:
    T func2();
};

template <typename T>
class TmplDerived: public TmplInterface<T>, TmplNotInterface<T>
{
public:
    T func() noexcept override { return t;}

    T t;
};

class TmplDerived<int> var;

In this example, the checker flags:

  • The non-interface class NotAnInterface, which acts as a base for the class IsDerived2. The class NotAnInterface is not an interface class because it contains a nonvirtual member function.

  • The template non-interface class TmplNotInterface, which acts as a base for the class TmplDerived. Note that TmplDerived also derives from the interface class TmplInterface, which is compliant with the rule.

Check Information

Group: Derived Classes
Category: Advisory, Non-automated

Version History

Introduced in R2021b