$ cat inheritance.cpp
#include
using namespace std;
class A { };
class B : private A { };
int main() {
A* ab = new B;
}
$
$ g++ inheritance.
clang++ gives a slightly more understandable error message:
example.cpp:9:13: error: cannot cast 'B' to its private base class 'A'
A* ab = new B;
^
example.cpp:6:11: note: declared private here
class B : private A { };
^~~~~~~~~
1 error generated.
I'm not a C++ expert, but it looks like it's just simply not allowed. I'll go poke around the spec and see what I come up with.
Edit: here's the relevant reference from the spec - Section 4.10 Pointer conversions, paragraph 3:
A prvalue of type "pointer to cv
D", whereDis a class type, can be converted to a prvalue of type "pointer to cvB", where B is a base class ofD. IfBis an inaccessible or ambiguous base class ofD, a program that necessitates this conversion is ill-formed.