9

Possible Duplicate:
Difference between private, public and protected inheritance in C++

What is difference between deriving as protected or private in c++? i am not able to figure out, since both seem to restrict base class member access from derived class object

Community
  • 1
  • 1
paseena
  • 4,207
  • 6
  • 32
  • 51
  • To be frank, both are a bit of a waste of space. In over 25 years of professional C++ programming, I've never used either in anger. –  May 05 '11 at 22:06
  • 1
    @unapersson: I've never used protected inheritance, but I've used private inheritance before. A class inherits publicly from a read-only interface, and privately from a read-write interface. It can then pass itself to functions expecting the read-write interface, but any other class or function must use the read-only interface. Saves declaring a friend. Used it in an implementation of the Builder pattern. – Daniel Gallagher May 05 '11 at 22:20
  • 1
    @Daniel For me, composition will always win over private inheritance, as it reduces coupling. But YMMV. Protected inheritance has always been a bolt on - I seem to remember that it was added sometime around cfront 2.x simply because somebody thought public/protected/private access should be mirrored in the inheritance specifiers. But maybe my memory is playing tricks. –  May 05 '11 at 22:35
  • @unapersson: Private inheritance is occasionally useful for deriving from things like boost::noncopyable. That is, when you're trying to hide an interface (copy ctor, etc.) rather than expose one. – Drew Hall May 05 '11 at 23:08
  • @unapersson: I have posted a [question](http://codereview.stackexchange.com/questions/2268/composition-or-private-inheritance-for-implementing-builder) to codereview. I'm interested in your comments and critique of my technique. – Daniel Gallagher May 05 '11 at 23:09
  • 1
    I'd like to point out that an important use of private inheritance is to enable the empty base optimization. Apart from this I feel quite comfortable never using it. – Luc Danton May 06 '11 at 03:19
  • @Luc: I agree (for Policy Based Design), one more reason to feel strongly against that non-zero size for all objects :/ – Matthieu M. May 06 '11 at 12:58

5 Answers5

9

Let's consider a code example showing what would be allowed (or not) using different levels of inheritance:

 class BaseClass {};

 void freeStandingFunction(BaseClass* b);

 class DerivedProtected : protected BaseClass
 {
     DerivedProtected()
     {
         freeStandingFunction(this); // Allowed
     }
 };

DerivedProtected can pass itself to freeStandingFunction because it knows it derives from BaseClass.

 void freeStandingFunctionUsingDerivedProtected()
 {
     DerivedProtected nonFriendOfProtected;
     freeStandingFunction(&nonFriendOfProtected); // NOT Allowed!
 }

A non-friend (class, function, whatever) cannot pass a DerivedProtected to freeStandingFunction, because the inheritance is protected, so not visible outside derived classes. Same goes for private inheritance.

 class DerivedFromDerivedProtected : public DerivedProtected
 {
     DerivedFromDerivedProtected()
     {
         freeStandingFunction(this); // Allowed
     }
 };

A class derived from DerivedProtected can tell that it inherits from BaseClass, so can pass itself to freeStandingFunction.

 class DerivedPrivate : private BaseClass
 {
      DerivedPrivate()
      {
          freeStandingFunction(this); // Allowed
      }
 };

The DerivedPrivate class itself knows that it derives from BaseClass, so can pass itself to freeStandingFunction.

class DerivedFromDerivedPrivate : public DerivedPrivate
{
     DerivedFromDerivedPrivate()
     {
          freeStandingFunction(this); // NOT allowed!
     }
};

Finally, a non-friend class further down the inheritance hierarchy cannot see that DerivedPrivate inherits from BaseClass, so cannot pass itself to freeStandingFunction.

Daniel Gallagher
  • 6,915
  • 25
  • 31
  • So there *are* "useful" applications of protected / private inheritence. Thanks for the nice examples. – Xeo May 06 '11 at 04:40
3

Use this matrix (taken from here) to decide the visibility of inherited members:

inheritance\member  |     private     |   protected   |   public
--------------------+-----------------+---------------+--------------
private             |  inaccessible   |    private    |  private
protected           |  inaccessible   |    protected  |  protected
public              |  inaccessible   |    protected  |  public
--------------------+-----------------+---------------+--------------

Example 1:

class A { protected: int a; }
class B : private A {};       // 'a' is private inside B

Example 2:

class A { public: int a; }
class B : protected A {};     // 'a' is protected inside B

Example 3:

class A { private: int a; }
class B : public A {};        // 'a' is inaccessible outside of A
Peter Jankuliak
  • 3,464
  • 1
  • 29
  • 40
1

private allows only the class it is declared in to access it protected allows that class and derived/sub classes to access as if it were private

Meberem
  • 937
  • 7
  • 18
1

I had added a very detailed explanation about Inheritance & Access Specifiers in this Q. It explains all the types of Inheritance and how access specifiers work with each of them. Do have a look.
Hth :)

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

Basically, protected inheritance extends further down the inheritance hierarchy than does private inheritance. See the C++ FAQ Lite for details.

Lstor
  • 2,265
  • 17
  • 25