提高::绑定返回一个函数对象,它是一个需要指针的函数参数 [英] boost::bind return a function object which is the argument for a function that requires pointer
问题描述
我在做C ++在Linux上的编码有关的boost ::绑定。
I am doing C++ coding on Linux about boost::bind.
的boost ::绑定的返回数据类型是一个函数对象,这是一个输入参数另一个函数bridge_set_pound_var_func。
The return data type of boost::bind is a function object, which is an input argument to another function bridge_set_pound_var_func.
不过,bridge_set_pound_var_func的输入参数必须是一个函数指针。 bridge_set_pound_var_func的接口不能被改变。
But, bridge_set_pound_var_func's input argument must be a function pointer. bridge_set_pound_var_func's interface cannot be changed.
在code是如下:
#include <boost/bind.hpp>
#include <iostream>
using namespace boost;
class myA
{
public:
int bridge_set_pound_var_func( int (*pound_var_func)(const char *, char *, void *), void *arg ) { std::cout << "bridge_set_pound_func is called " << std::endl ; return 0; }
};
class myC
{
public:
myA *myOA;
int func(const char * poundVar , char * t1, void * t2);
int myCCall() { myOA->bridge_set_pound_func( (boost::bind(&myC::func, this)), (void *)this ); return 0;}
};
int myC::func(const char * poundVar , char * t1, void * t2)
{
std::cout << "myC::func is called " << std::endl;
return 1;
}
int main()
{
myC myCO ;
myC *m1p = &myCO ;
m1p->myCCall() ;
return 0 ;
}
// EOF
我得到了编译错误:
I got compile error:
error: no matching function for call to
'myA::bridge_set_pound_func(boost::_bi::bind_t<int (&)(const char*, char*, void*), boost::_mfi::dm<int ()(const char*, char*, void*), myC>, boost::_bi::list1<boost::_bi::value<myC*> > >, void*)'
note: candidates are: int myA::bridge_set_pound_func(int (*)(const char*, char*, void*), void*)
任何帮助将AP preciated。
Any help will be appreciated.
这是新的code的工作。但是,MYC :: func被称为不打印,为什么?
This is the new code that work. But, "myC::func is called" is not printed, why ?
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
using namespace boost;
class myA
{
public:
int bridge_set_pound_var_func( const boost::function3<int, const char *, char *, void *> f, void *arg ) { std::cout << "bridge_set_pound_var_func is called " << std::endl ; return 0; }
};
typedef int (*funcPtr)(const char *, char *, void *) ;
typedef boost::function0<int&> boostBindFuncType;
class myC
{
public:
myA *myOA;
int func(const char * poundVar , char * t1, void * t2);
int myCCall()
{
std::cout << "myCCall is called " << std::endl;
myOA->bridge_set_pound_var_func( (boost::bind(&myC::func, this, _1, _2, _3)), (void *)this );
return 0;
}
};
int myC::func(const char * poundVar , char * t1, void * t2)
{
std::cout << "myC::func is called " << std::endl;
return 1;
}
int main()
{
myC myCO ;
myC *m1p = &myCO ;
m1p->myCCall() ;
return 0 ;
}
我不能改变bridge_set_pound_var_func的接口,它是由许多其它的功能调用。是否有可能改变的boost ::绑定返回函数对象的函数指针?
I cannot change the interface of bridge_set_pound_var_func, which is called by many other functions. Is is possible to transform boost::bind returned function object to a function pointer?
推荐答案
传统的最后无效*
传递给回调参数是用户定义的数据。如果是这种情况下,你可以创建一个辅助功能,可以让你通过仿函数。唯一的问题是,你需要确保存在用户数据,直到回调将不再被称为 - 这将取决于你的程序结构很多。我只是为那以确保其继续存在的最简单方法泄漏的对象。 (虽然你也有与含 MYC
对象是否存在等问题)。
Traditionally the final void *
argument passed to callbacks is user defined data. If this is the case for you you can create a helper function that will let you pass functors. the only trouble is that you need to ensure that the user data exists until the callback will no longer be called - and that will depend a lot on your program structure. I'm just going to leak the object as thats the easiest way to ensure it continues to exist. ( Though you'll also have problems with the existance of the containing myC
object ).
class myC
{
public:
myA *myOA;
//NOTE: I've removed the void* ptr here, which should be user data.
// If you still need it you'll need to bind it away at functor creation time.
int func(const char * poundVar , char * t1);
int myCCall()
{
//TODO: Fix this intentional leak.
boost::function<int(const char*, char*)> * fn = new boost::function<int(const char*,char*)>( boost::bind(&myC::func,this) );
//Call our helper function instead of `set_pound_func`.
set_pound_func_with_functor(myOA, fn );
return;
}
};
// Function that is really passed to `set_pound_func` when
// set_pound_func_with_functor is called.
// It converts the user data back to a boost function and calls it.
int pound_func_with_functor(const char* d1, char* d2, void* user_data)
{
boost::function<int(const char*,char*)> * fn = static_cast< boost::function<int(const char*, char*) >( user_data );
return (*fn)(d1,d2);
}
//Helper function to make set_pound_func work with boost functions instead.
//NOTE: You are required to ensure the fn argument exists for long enough...
void set_pound_func_with_functor( myA * myOA, boost::function<int(const char *, char *)> & fn )
{
myOA->bridge_set_pound_var_func( £_func_with_functor, &fn );
}
这篇关于提高::绑定返回一个函数对象,它是一个需要指针的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!