Is it possible to inherit from boost :: function?

I would like to know if it is possible to inherit from boost :: function.

Basically, for ease of use, what I would like to have is the "Delegate" type, which is basically a boost :: function. This is just for ease of use in some code that I am writing.

I am at some point typedef'd boost :: function to Delegate, but typedef'ing in my experience is playing hell with gdb. Especially if it was modified, so I wanted to avoid this (ever trying to debug stl containers that were typed? Oofta).

I found code on the Internet that gave an example:

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

Now when I try to use it, I get some errors. Usage example:

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

It gives errors like

 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()()>&) 

If I had to guess why I get these errors, I have to say that it is not, some kind of copy constructor that accepts any base that the boost :: bind constructor returns.

Any thoughts on how I can overcome this obstacle, or anyone who can point me to good examples of inheritance from boost :: function?

+4
source share
2 answers

Class output does not automatically inherit base class constructors for a derived class. You will need to create all the necessary constructors there.

+6
source

hKaiser was right when I needed to write the necessary constructors.

I had from time to time until I found the interface file for the boost class function on my website.

In the end, I got 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 use dynamic_cast correctly (in the context of following good coding rules), or if I even need it in the assignment operator, but it works and works very well.

0
source

Source: https://habr.com/ru/post/1343104/


All Articles