开发者

Is it possible to inherit from boost::function?

开发者 https://www.devze.com 2023-02-15 18:27 出处:网络
I\'d like to know if it\'s possible to inherit from boost::function. Basically, for ease of use, what I\'d like to

I'd like to know if it's possible to inherit from boost::function.

Basically, for ease of use, what I'd like to is have a type "Delegate" which is basically a boost::function. It's just for ease of use in some code I'm writing.

I at one point typedef'd boost::function to Delegate, but typedef'ing in my experience plays hell with gdb stuff. Especially if it's templated, so I wanted to avoid that (ever try debugging stl containers that've been typdeffed? oofta).

I found some code online which gave some sort of an example:

template<class Signature>
class Delegate : public boost::function<Signature>
{
public: 
    using boost::function<Signature>::operator();
};

Now, as I attempt to use it I get some errors. A usage example would be:

Tank * tankptr = new Tank();
Delegate<void ()> tankShoot(boost::bind(boost::mem_fn(&Tank::Shoot),tankptr));

This yields errors such as

error: no matching function for call to ‘Delegate<void ()()&开发者_如何转开发gt;::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()()>&)

If I had to guess why I'm getting these errors, I'd have to say it's cause I'm missing some kind of copy constructor that takes whatever base a boost::bind constructor returns.

Any thoughts on how I can get past this hurdle, or anyone able to point me to good examples of inheriting from boost::function?


Deriving from a class does not automatically 'inherit' the base class constructors for the derived class. You will need to create all required constructors there.


hKaiser was correct in my needing to write the required constructors.

I had a hell of a time of it, until I found the interface file for the boost class "function" on their website.

In the end I ended up with something like:

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

I'm not sure if I'm properly using the dynamic_cast (in the context of adhering to good coding practices) or if I even need it there in the assignment operator, but it does work, and works extremely well.

0

精彩评论

暂无评论...
验证码 换一张
取 消