I would like to access Base
class members in an unqualified way (why? macro sorcery) from outside of the class itself. The strategy is do it in Derived
class and cast the pointer-to-Base
to pointer-to-Derived
(even though the instance is not a Derived
).
The code compiles and runs correctly as far as I tried: is this by standard or by accident (and UB by standard)? coliru link
#include<iostream>
struct Base{
int a=3;
};
struct Derived: public Base{
int getA(){
// this scope has unqualified access to Base class members
return a;
}
};
int main(void){
Base b;
std::cerr<<((Derived*)(&b))->getA()<<std::endl;
}