How do I access overridden members of base classes of base classes?
#include <iostream>
using namespace std;
class A {public: char x; A(){x='A';};};
class B1 : public A {public: char x; B1(){x='B';};};
class B2 : public A {public: char x; B2(){x='B';};};
class C : public B1, public B2 {public: char x; C(){x='C';};};
int main(){
C c;
cout << c.x << endl; // prints C
cout << c.B1::x << endl; // prints B
cout << ((B1&) c).x << endl; // prints B
// cout << c.A::x << endl; // normally prints A but doesn't work here
cout << ((A&) c).x << endl; // prints A
return 0;
}
Is the reference (or pointer) way the only possibility? I tried A::B::x in order to chain scope operators, but that doesn't work. Suppose i want to keep the "double A", i.e. not make the inheritance virtual.
((B&) c).x
seems to be a good "workaround" to c.B::x
but they aren't equal in case of virtual functions, are they?