Why does C style cast allow you to convert to a private base class?
c++, casting, class, upcasting
Solution
It's because in C it was allowed to convert any pointer to any other pointer using this cast and C++ tries to be C-compatible as much as possible, but tries to do a good job to be correct when it comes to classes, so C style cast is stronger than `reinterpret_cast` in this situation.
Problem
Say we have this code ``` class A { public: A() : x(1) {} virtual ~A() {} int x; }; class B { public: B() : y(2) {} virtual ~B() {} void g() { cout << "B::" << y << endl; } int y; }; class C : private A, private B { public: void f() { B* p = static_cast<B*>( this ); p->g(); } }; int main() { C c; ((B*)&c)->g(); return 0; } ``` The C style cast in the main function cannot be correctly expressed in terms of the C++ casts (`static_cast`, `dynamic_cast`, `reinterpret_cast`). But what is the reason to allow this in the first place? Doesn't it hurt encapsulation? UPDATE This is not a duplicate of the linked question, because this question is about design decisions in C++. It does not ask what I can or cannot do with the language, it asks why certain decisions might have been made.