Boost::bind 'Call to empty boost::function'

2019-08-31 16:28发布

问题:

Why might using the following fail unless called from the constructor of the class?

    updateState = boost::bind( &PhysicsObject::updateActive, this );

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 );
}
}

回答1:

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.



标签: c++ boost