0

我正在使用信号2。我正在尝试使用具有订阅槽的视图设置视图状态/视图关系。我似乎无法触发处理程序函数。绑定有问题吗?我是 C++ 新手,所以可能误用了 const/reference/dereferencer。

在我的状态机中:

void State::setAppState( State::AppState pNewState )
{
    mCurrentState = pNewState;
    // this prints fine
    ci::app::console() <<"\nState::setState " << pNewState << "\n";
    (*mSignal)();
}

在我看来基类:

BaseView::BaseView( State& pState ): mState(pState)
{
    // register connection to state
    mConnection = mState.connect(boost::bind(&BaseView::stateChange, this));
}

// the use of const is right from their example in the doc.
// but i found i had to const_cast to get it to compile
// can i get rid of the 'this' and do it without const method? 
// edit: no (boost compile errors)
void BaseView::stateChange()  const
{
    int s = const_cast<State&>(mState).getAppState();
    // this does not print
    ci::app::console() << "\n>>>>> View Slot registered change " << s << "\n" ;
}

在我看来子类:

AttractView::AttractView(State pState):BaseView(pState)
{
     // to pass the constructor param
}

主应用:

mAttract = AttractView( mStateMachine );
mStateMachine.setAppState(State::AppState::Attract); //AppState is just an enum
4

1 回答 1

0

我认为问题实际上出在初始化列表上。

虽然 pState 在 BasiView 构造函数中是有效的,但它没有正确绑定,或者它在信号初始化之前被绑定并且绑定被擦除。

 BaseView::BaseView( State& pState ): mState(pState)

添加一个额外的 init/create 函数可以解决这个问题。

void BaseView::init(State& pState){
    mConnection = mState.connect( (boost::bind(&BaseView::stateChange, this)) );
}

...

mAttract.init( mStateMachine );
mStateMachine.setAppState(State::AppState::Attract);
于 2013-09-10T23:04:46.400 回答