1

I am going through a book on Design Patterns by GoF - online link.

In this book, in Adapter pattern, in Sample Code section, I have come across this particular code:

class TextView {
     public:
         TextView();
         void GetOrigin(Coord& x, Coord& y) const;
         void GetExtent(Coord& width, Coord& height) const;
         virtual bool IsEmpty() const;
}; 

This class TextView is privately inherited by TextShape as below:

  class TextShape : public Shape, private TextView {
     public:
         TextShape();
              virtual void BoundingBox(
             Point& bottomLeft, Point& topRight
         ) const;
         virtual bool IsEmpty() const;
         virtual Manipulator* CreateManipulator() const;
  };

Then in this void TextShape::BoundingBox function as below:

   void TextShape::BoundingBox (
         Point& bottomLeft, Point& topRight
     ) const {
         Coord bottom, left, width, height;

         GetOrigin(bottom, left);  //How is this possible? these are privately inherited??
         GetExtent(width, height); // from textView class??
         bottomLeft = Point(bottom, left);
         topRight = Point(bottom + height, left + width);
     }

As one can see, the functions GetExtent & GetOrigin is called TextShape, whereas, the class TextView containing these was inherited privately.

My understanding is that, in private-inheritance, all the parent class members become inaccessible, so how is this (void TextShape::BoundingBox()) function trying to access it?

UPDATE:

Thanks all for answering, I had got into a wrong notion while reading about private inheritance. I felt, it would even prevent access to any of the members, whereas practically it changes the access-specifiers and not accessibility. Thanks you very much for clarifying :)

linuxeasy
  • 6,269
  • 7
  • 33
  • 40
  • *"My understanding is that, in private-inheritance, all the parent class members become inaccessible"*: What would be gained by inheritance then if you cannot access any base members in the derived class? – Christian Rau Mar 03 '12 at 10:52

2 Answers2

3

During private inheritance, the parent members become inaccessible for everyone else, not the class itself! The protected and public members of the parent class are still accessible in the derived class and friends thereof.

Example:

class Base
{
    public: int publ;
    protected: int prot;
    private: int priv;
};

class Derived: private Base
{
    friend void g();
    void f()
    {
         priv = 42; //error, priv is private
         prot = 42; //OK
         publ = 42; //OK 
    }
};

int main()
{
    Derived d;
    d.priv = 42; //error
    d.prot = 42; //error
    d.publ = 42; //error
}

void g()
{
    Derived d;
    d.priv = 42; //error
    d.prot = 42; //OK
    d.publ = 42; //OK
}
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
3

Here's how inheritance affects access:

  • Public inheritance makes public base members public, protected members protected, and private base members are inaccessible.

  • Protected inheritance makes public and protected base members protected within the derived class, and private base members are inaccessible.

  • Private inheritance makes public and protected base members private within the derived class, and private base members are inaccessible.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Oh, so it's like they become private in the derived class but not inaccessible, is that understanding correct? – linuxeasy Mar 03 '12 at 11:00
  • 1
    @linuxeasy: If you're speaking about protected and public members, then yes, your understanding is correct. Private members are never accessible in the derived class, regardless of the inheritance type (unless the derived class is declared friend to the base class, which is rare) – Armen Tsirunyan Mar 03 '12 at 11:08
  • @ArmenTsirunyan: Thanks, I had got into a wrong notion while reading about private inheritance. I felt, it would even prevent access to any of the members, whereas practically it changes the access-specifiers and not accessibility. Thanks you very much for clarifying :) – linuxeasy Mar 03 '12 at 11:17
  • @KerrekSB: This answer was very helpful for clarifying the concept too. Thanks a lot again. – linuxeasy Mar 03 '12 at 11:20