-2

I'm trying to understand the private inheritance concept.

So far everywhere I see they write that private inheritance makes its members inaccessible from the derived class.

Doesn't this make it sort of useless? If I can't access the class inherited, what is the purpose of deriving in the first place?

Now I know private classes are actually used and helpful. I'm just having trouble in understanding how.

Anonymous Guy
  • 119
  • 2
  • 11
  • 1
    You might be interested in [this thread](https://stackoverflow.com/questions/656224/when-should-i-use-c-private-inheritance). – lubgr Sep 17 '18 at 08:50
  • "everywhere I see they write" Who? I have never seen such a claim. Please quote what you are reading. – n. m. could be an AI Sep 17 '18 at 08:54
  • "Doesn't this make it sort of useless?" - no. The member can still be used by calling a method from the father class which uses it. Essentially, this says "this member is for a functionality that is not to be ever touched by any child class". – Aziuth Sep 17 '18 at 08:59

3 Answers3

1

It is indeed rare to want private inheritance, but sometimes you do want users of your class to not be able to reach members of the base class.

For example, I use Boost (www.boost.org) for my date class, but I don't want that exposed to users of my date class, since one day I might want to switch out Boost for something else.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Your question reads as if private members would be useless altogether. I mean you could as well ask, what is a private member good for if it cannot be accessed from outside. However, a class (usually) has more than what a subclass can use.

Consider this simple example:

struct foo {
    void print(); 
};

struct bar : private foo {
    void print_bar() {
        std::cout << " blablabla \n";
        print();
        std::cout << " bblablabla \n";
    }
};

A class inheriting from bar wont even notice that bar inherits from foo. Nevertheless, it does make sense for bar to inherit from foo, because it uses its functionality.

Note that private inheritance is actually closer to composition rather than public inheritance, and the above could also be

struct bar { 
    foo f;
    void print_bar() {
        std::cout << " blablabla \n";
        print();
        std::cout << " bblablabla \n";
    }
};

How do I access privately inherited class members in C++?

Outside of the class: You dont, thats what the private is for. It's not different for private members or private methods in general, they are there for a reason, you just cannot access them from outside.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

Private inheritance is a way to model a has-a relationship. The usual alternative to model this is composition, which should be used when possible. Private inheritance is useful to model the has-a relationship when protected members of a class are involved.

A different way of framing it is "we want to use the implementation of this class, but ignore its interface".

(My experience with this is limited and this is what I recall from Scotty Meyers Effective C++, as there never arose a case where I would have prefered private inheritance over composition)

Ketzu
  • 660
  • 1
  • 7
  • 12