Why does C style cast allow you to convert to a private base class? [duplicate]
问题 This question already has an answer here: Can I cast a derived class to a private base class, using C-style cast? 3 answers 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