Boost function question.
Hello, I'm having the following class structure. template <class T> class TemplatedClass { TemplatedClass () { member = shared_ptr<T> (CreateA()) ; } private: shared_ptr<T> member ; operator->() { return T.get() ; } } ; class BasePlugin { virtual void foo (void) = 0 ; } class DerivedPlugin_A : public BasePlugin { virtual void foo (void) { cout << "FOO in derived A" << endl ;} } ; DerivedPlugin_A* CreateA () { return new DerivedPlugin_A ; } TemplatedClass <BasePlugin> t ; t->foo() ; How do i write a boost::function wrapper for t->foo() ; Please help. Thanks in advance, Surya
Surya Kiran Gullapalli wrote:
template <class T> class TemplatedClass { TemplatedClass () { member = shared_ptr<T> (CreateA()) ; }
private: shared_ptr<T> member ;
operator->() { return T.get() ; } } ;
class BasePlugin { virtual void foo (void) = 0 ; }
class DerivedPlugin_A : public BasePlugin { virtual void foo (void) { cout << "FOO in derived A" << endl ;} } ;
DerivedPlugin_A* CreateA () { return new DerivedPlugin_A ; }
TemplatedClass <BasePlugin> t ; t->foo() ;
How do i write a boost::function wrapper for t->foo() ;
As far as I understand it you need to double nest the binds. Using Boost.Lambda it would be something like this: boost::lambda::bind( &BasePlugin::foo, boost::lambda::bind( &TemplatedClass< BasePlugin >::operator->, t ) ) This expression should give you a boost::function< void ( void ) >. K
As far as I understand it you need to double nest the binds. Using Boost.Lambda it would be something like this:
boost::lambda::bind( &BasePlugin::foo, boost::lambda::bind( &TemplatedClass< BasePlugin >::operator->, t ) )
This expression should give you a boost::function< void ( void ) >.
K
That doesn't seem to be working. I'm getting lot of errors and its next to impossible to comprehend those errors. I've attached sample files to what I'm trying to do. Please Help. Thanks in advance, Surya
Surya Kiran Gullapalli
As far as I understand it you need to double nest the binds. Using Boost.Lambda it would be something like this:
boost::lambda::bind( &BasePlugin::foo, boost::lambda::bind( &TemplatedClass< BasePlugin >::operator->, t ) )
This expression should give you a boost::function< void ( void ) >.
K
That doesn't seem to be working. I'm getting lot of errors and its next to impossible to comprehend those errors.
I've attached sample files to what I'm trying to do.
Please Help.
Thanks in advance, Surya
You can do it hard way and easy way.
1. Hard way.
fp = boost::lambda::bind
(
&BaseClass::foo,
boost::lambda::bind(&TemplateClass<BaseClass>::operator->, t),
boost::lambda::_1
);
2. Easy way.
a) Add the following to template.h.
namespace boost
{
template<class T>
T * get_pointer(const TemplateClass<T> & p)
{
return p.operator->();
}
}
b) Use boost::bind instead of boost::lambda.
//#include
participants (3)
-
Kirit Sælensminde
-
Roman Perepelitsa
-
Surya Kiran Gullapalli