Private inheritance, despite the name, really is not inheritance - at least not from the outside (of the class) where it matters.
For this reason, different rules apply. In C ++, it is believed that private inheritance models "implemented in terms of" relationships. Thus, the priority queue, which is implemented as a heap, may look like this:
template <typename T, typename Comp = std::less<T> > class priority_queue : private heap<T, Comp> {
Personally, I do not see the advantages of this template, and Neil has already stated that in most cases the composition actually has an advantage over private inheritance.
However, there is one advantage: starting with such an established template, the value of private inheritance immediately becomes clear to an experienced C ++ programmer; the code above would tell them that the priority queue is implemented in terms of heap - which would not be obvious if the class simply used the heap as one of its members.
Private inheritance is typically used in C ++ mainly for policy classes. A classic example is allocators, which determine how a container class manages storage inside:
template <typename T, typename A = std::allocator<T> > class vector : private A {
No harm done. But once again this could be done using the composition.
source share