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; }
C styles in the main function cannot be correctly expressed in terms of C ++ translations ( static_cast , dynamic_cast , reinterpret_cast ). But what is the reason for this in the first place? Does encapsulation hurt?
UPDATE This is not a duplicate of the related question, because this question is about design decisions in C ++. He does not ask what I can or cannot do with the language, he asks why some decisions could be made.
unkulunkulu
source share