I always assumed that private inheritance simply means that a type doesn't tell the outside that it's inheriting from some base class. However, it seems that there are more restrictions.
Consider the following minimal example:
struct MyInterface {};
struct MyImpl : private MyInterface {};
struct Inherited : public MyImpl {
// Error: 'MyInterface' not accessible because 'MyImpl' uses 'private' to inherit from 'MyInterface'
void doSomething(MyInterface* mi) {}
};
struct Noninherited {
// All fine!
void doSomething(MyInterface* mi) {}
};
Clang, GCC, and MSVC all reject this code. Given my previous assumptions, I would have expected it to be fine.
doSomething
simply expects a pointer to MyInterface
, but it doesn't tell the outside world that Inherited
has MyInterface
in its inheritance hierarchy. To me, it seems that private inheritance doesn't only not tell the outside world about the inheritance structure but instead makes the whole inheritance structure completely "forget" that the inherited type even exists.
Is this the right "mental model" to understand private inheritance? Are there other unexpected restrictions to it?
MyImpl:: MyInterface
, in the second case you are accessing public nameMyInterface
– Butcherbird