Using boost function with boost bind with a map

前端 未结 1 854
梦如初夏
梦如初夏 2020-12-12 05:31

In this code, the line (iter->second)(); calls the function reset(new childClass()). Is this correct ? If this is the case, it is called on whi

相关标签:
1条回答
  • 2020-12-12 06:11

    the line (iter->second)(); calls the function reset(new childClass()). Is this correct?

    No, it invokes reset(). If you want it to be called as reset(new childClass()), you need to pass new childClass() to boost::bind as argument like

    boost::bind( static_cast<void( boost::shared_ptr<BaseClass>::* )(BaseClass*)>( 
                   &boost::shared_ptr<BaseClass>::reset ), 
                 boost::shared_ptr<BaseClass>(new childClass() ),
                 new childClass() )
    

    Note that new childClass() itself is evaluated when boost::bind is invoked, not the functor is invoked.

    Or you can add a placeholder, and pass new childClass() when call the functor.

    it is called on which object ?

    It's called on the object copied from the argument passed to boost::bind, i.e. boost::shared_ptr<BaseClass>(new childClass() ).

    0 讨论(0)
提交回复
热议问题