I used to feel that the private inheritence is useless. Although we can
implement the has-a semantics with private inheritence, it provides little
benefits compared with object composition. Besides, in order to expose the
privately inherited members to public, C++ introduced an awkward syntax, i.e.
the using
directives. These are the reasons why I didn't like the
private inheritence at all.
Recently, I changed my mind. Since we only have to specify the function
names in the using
directives, all of the overloaded functions can
be exported with one directive. As a result, we can reduce the numbers of
helper functions. For example:
#include <iostream>
class A {
public:
void test() { }
void test() const { }
void test(int i) { }
void test(int i) const { }
void test(int i, int j) { }
void test(int i, int j) const { }
};
class B : private A {
public:
using A::test;
};
int main() {
// All of these should work.
B b;
b.test();
b.test(1);
b.test(1, 2);
const B &rb = b;
rb.test();
rb.test(1);
rb.test(1, 2);
}
However, IMHO, private inheritence is still a good indication of bad class hierarchy deisgn.