我正在尝试使用boost::function
和boost::bind
创建功能图:
typedef std::map<std::string, boost::function<void( boost::shared_ptr<BaseClass> )>> Map_t;
Map_t g_cmdMap = map_list_of( "cmd1", boost::bind( static_cast<void( boost::shared_ptr<BaseClass>::* )()>( &boost::shared_ptr<BaseClass>::reset ), new childClass1() ) )
( "cmd2", boost::bind( static_cast<void( boost::shared_ptr<BaseClass>::* )()>( &boost::shared_ptr<BaseClass>::reset ), new childClass2() ) )
( "cmd3", boost::bind( static_cast<void( boost::shared_ptr<BaseClass>::* )()>( &boost::shared_ptr<BaseClass>::reset ), new childClass3() ) );
这将在以后像这样使用:
std::map<std::string, boost::function<void( boost::shared_ptr<BaseClass> )>>::iterator iter;
iter = g_cmdMap.find( cmdName );
iter->second.reset();
我遇到以下错误:
错误C2440'-> *':无法将'ChildClass *'转换为 'boost :: shared_ptr
*'
更新:
是否可以使用参数new ChilBase将重设的函数绑定到对象BaseClass上:
boost::shared_ptr<BaseClass> pObjectClass;
typedef std::map<std::string, boost::function<void()>> g_cmdMap = map_list_of( "cmd", boost::bind( static_cast<void(boost::shared_ptr<BaseClass> ::* )()>( &boost::shared_ptr<BaseClass> ::reset ), pObjectClass, boost::shared_ptr<BaseClass>( new ChildClass() ) ) );