I think the issue is an oversite in the standard. There's no reason why:
State A::*state = static_cast<State A::*>( &A::s1 );
shouldn't work, but the wording to allow it is missing in the
standard. There are a number of work-arounds: the most obvious
would be to have an accessor function returning a State*
for
each member variable, and use a pointer to the function:
class A
{
public:
S1 s1;
State* getS1() { return &s1; }
S1 s2;
State* getS2() { return &s2; }
};
class B
{
static A a;
static State* (A::*getState)();
public:
void Do() const
{
(a.*getState)()->Do();
}
}
State* (A::* B::getState)() = &A::getS1;