【问题标题】:Is it possible to inherit from boost::function?是否可以从 boost::function 继承?
【发布时间】:2011-03-10 03:06:33
【问题描述】:

我想知道是否可以从 boost::function 继承。

基本上,为了便于使用,我想 有一个“委托”类型,它基本上是一个 boost::function。 这只是为了在我正在编写的一些代码中易于使用。

我曾一度将 boost::function 类型定义为 Delegate,但根据我的经验,typedef 对 gdb 的东西非常不利。特别是如果它是模板化的,所以我想避免这种情况(曾经尝试调试已经被 typdeffed 的 stl 容器?oofta)。

我在网上找到了一些代码,其中给出了一些示例:

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 继承?

【问题讨论】:

    标签: c++ inheritance boost


    【解决方案1】:

    从类派生不会自动“继承”派生类的基类构造函数。您需要在那里创建所有必需的构造函数。

    【讨论】:

    • 我刚刚发布了一个类似的答案。他需要一个构造函数来接受 boost::function 并将其传递给基类。
    • 我想的也差不多。任何人都知道这样做的例子吗?我在语法上有问题,可以从一个很好的例子中受益。
    • 啊,我想我明白了。我不得不做类似template&lt;class Signature&gt; class Delegate : public boost::function&lt;Signature&gt; { public: using boost::function&lt;Signature&gt;::operator(); Delegate() : boost::function&lt;Signature&gt;() {} Delegate(const boost::function&lt;Signature&gt;&amp; x) : boost::function&lt;Signature&gt;(x) {} }; 的事情(我的格式很糟糕,抱歉)。
    • @sbrett:您也可以只实现模板 ctor template&lt;class Functor&gt; Delegate(Functor f) : boost::function&lt;Signature&gt;(f){} 并依赖 Boost.Function ctor。
    【解决方案2】:

    hKaiser 是正确的,因为我需要编写所需的构造函数。

    我玩得很开心,直到我找到了 boost 类“函数”的接口文件 在他们的网站上。

    最后我得到了类似的东西:

    template<class Signature>
    class Delegate : public boost::function<Signature>
    {
    public:
      ///use the functor operator from the original class
      using boost::function<Signature>::operator();
    
      ///base constructor for our new type, 
      Delegate() : boost::function<Signature>() {/*empty*/}
    
      ///copy constructor for our new type
      Delegate(const boost::function<Signature>& x) : boost::function<Signature>(x) {/*empty*/}
    
      Delegate& operator=(const Delegate & _delegate)
      {
        boost::function<Signature> x = _delegate;
        try
        {
          dynamic_cast<boost::function<Signature> & >(*this) = x;
        }
        catch(bad_cast &bc)
        {
          cout << "Bad Cast Exception. " << bc.what();
          int * ptr = NULL;
          *ptr = 1; //force seg fault instead of assert
        }
        return *this;
      }
    };
    

    我不确定我是否正确使用了 dynamic_cast(在遵守良好编码实践的情况下),或者我什至在赋值运算符中是否需要它,但它确实有效,而且效果非常好。

    【讨论】:

    • 想在此基础上再补充一点,虽然以上内容适用于编译器 gcc 4.1.1
    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2013-11-22
    • 1970-01-01
    相关资源
    最近更新 更多