Next: High Integrity CPP Rule 3.3.2  Up: 3.3 Inheritance  Previous: 3.3 Inheritance  Contents

High Integrity CPP Rule 3.3.1   Use public derivation only.
(QA C++  2193, 2194)

Justification

Using public derivation maintains visibility of public base members in an intuitive way. Public derivation indicates the "is-a" relationship. Private derivation indicates the "is-implemented-by" relationship, which can also be indicated by containment (that is, declaring a private member of that class type instead of inheriting from it). Containment is the preferred method for "is-implemented-by", as this leaves inheritance to mean "is-a" in all cases.

   
   class A {};
   
   class B : private A {};         // avoid private derivation
   
   class C : protected A {};       // avoid protected derivation
   
   class D : A {};                 // avoid implicitly private derivation 
   
   class E : public A {};          // prefer public derivation 
Reference

Effective C++ Item 42;


HICPP VERSION 2.4  http://www.codingstandard.com   Copyright: © 2007 THE PROGRAMMING RESEARCH GROUP