Boost :: bind仅当未在构造函数中设置时,“对空boost :: function的调用"才会失败 [英] Boost::bind 'Call to empty boost::function' fails only if not set in constructor
问题描述
为什么除非从类的构造函数中调用,否则使用以下命令为什么会失败?
Why might using the following fail unless called from the constructor of the class?
updateState = boost::bind( &PhysicsObject::updateActive, this );
但是,以下内容在运行时失败,并带有"what():对空boost :: function的调用"例外
However the following fails at runtime, with a 'what(): call to empty boost::function' exception
void PhysicsObject::setState( PhsyicsObjectState aState ) {
_state = aState;
if( _state == ACTIVE ) { // This branch is executed
updateState = boost::bind( &PhysicsObject::updateActive, this );
} else {
updateState = boost::bind( &PhysicsObject::updateExploding, *this );
}
}
推荐答案
调用未设置的boost::function
会引发此类异常.您应该根据默认的状态"在构造函数中对其进行初始化,否则,如果setState
传递的状态与当前状态相同,则不会设置它.
Calling a boost::function
that wasn't set would raise such exception. You should initialize it in your constructor according to the default "state", otherwise your setState
won't set it if passed a state same as the current.
请注意,在第二个绑定中,您正在传递
Note that in your second bind, you are passing a copy of the object pointed by this
.
这篇关于Boost :: bind仅当未在构造函数中设置时,“对空boost :: function的调用"才会失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!