10.3 Abstract classes

10.3.1 Ensure that a derived class has at most one base class which is not an interface class

An interface class has the following properties:

  • all public functions are pure virtual functions or getters, and
  • there are no public or protected data members, and
  • it contains at most one private data member of integral or enumerated type

Inheriting from two or more base classes that are not interfaces, is rarely correct. It also exposes the derived class to multiple implementations, with the risk that subsequent changes to any of the base classes may invalidate the derived class. On the other hand. it is reasonable that a concrete class may implement more than one interface.

#include <cstdint>
   
class A
{
public:
virtual ~A () = 0;
virtual void foo () = 0;
};
   
class B
{
public:
virtual ~B () = 0;
virtual void bar () = 0;
};
   
class C
{
public:
C ();
void foo ();
virtual ~C ();
   
private:
int32_t m_i;
};
   
// @@+ Compliant +@@
class D: public A, public B, public C
{
public:
~ D();
};
               
class E
{
public:
E ();
};
               
// @@- Non-Compliant -@@
class F : public E, public D
{
};

References

  • JSF AV C++ Rev C – 87
  • JSF AV C++ Rev C – 88
  • HIC++ v3.3 – 3.4.6

Click here for references