我想知道是否可以从boost :: function继承。
基本上,为了方便使用,我想要一个类型“Delegate”,它基本上是一个boost :: function。这只是为了在我编写的某些代码中更容易使用。
我曾经将boost :: function typedef为Delegate,但根据我的经验,typedef会对gdb造成严重影响。特别是如果它是模板化的,所以我想避免这种情况(您是否曾经尝试过调试已经被typedef的stl容器?噢,天啊)。
我在网上找到了一些代码,其中提供了某种示例:
现在,我尝试使用它时遇到了一些错误。一个使用例子如下:
这会导致诸如以下错误:
基本上,为了方便使用,我想要一个类型“Delegate”,它基本上是一个boost :: function。这只是为了在我编写的某些代码中更容易使用。
我曾经将boost :: function typedef为Delegate,但根据我的经验,typedef会对gdb造成严重影响。特别是如果它是模板化的,所以我想避免这种情况(您是否曾经尝试过调试已经被typedef的stl容器?噢,天啊)。
我在网上找到了一些代码,其中提供了某种示例:
template<class Signature>
class Delegate : public boost::function<Signature>
{
public:
using boost::function<Signature>::operator();
};
现在,我尝试使用它时遇到了一些错误。一个使用例子如下:
Tank * tankptr = new Tank();
Delegate<void ()> tankShoot(boost::bind(boost::mem_fn(&Tank::Shoot),tankptr));
这会导致诸如以下错误:
error: no matching function for call to ‘Delegate<void ()()>::Delegate(boost::_bi::bind_t<boost::_bi::unspecified, boost::_mfi::mf0<void, Tank>, boost::_bi::list1<boost::_bi::value<Tank*> > >)’
Delegate.h:26: note: candidates are: Delegate<void ()()>::Delegate()
Delegate.h:26: note: Delegate<void ()()>::Delegate(const Delegate<void()()>&)
如果我得猜测为什么我收到这些错误,我会说是因为我缺少一个拷贝构造函数,该拷贝构造函数可以接受boost::bind构造函数返回的任何基类。
你有什么想法可以帮我克服这个障碍吗?或者能否向我指出继承自boost::function的好例子?
using boost::function::operator();
Delegate() : boost::function() {}
Delegate(const boost::function& x) : boost::function(x) {}
}; `(我的格式很糟糕,抱歉)。 - sbretttemplate<class Functor> Delegate(Functor f) : boost::function<Signature>(f){}
并依赖于 Boost.Function 构造函数。 - Xeo