简体   繁体   English

使用boost :: bind传递成员函数

[英]Passing member function using boost::bind

struct AAA {
    char* myTraceProc(ClientData clientData, Tcl_Interp* interp, const char* name1, const char* name2, int flags) {
        return NULL;
    }
};

int main(int argc, char* argv[]) {
    Tcl_FindExecutable(argv[0]);
    Tcl_Interp* interp = Tcl_CreateInterp(); 

    AAA obj;
    boost::function<char*(ClientData, Tcl_Interp*, const char*, const char*, int)> f = boost::bind(&AAA::myTraceProc, &obj, _1, _2, _3, _4, _5);

    Tcl_TraceVar(interp, "database", TCL_TRACE_WRITES, f, 0);

    return 0;
}

In this code I have tried to pass AAA::myTraceProc to Tcl_TraceVar which accepts a function pointer with the same interface as it but I am getting this error. 在这段代码中,我试图将AAA::myTraceProc传递给Tcl_TraceVar ,它接受一个具有相同接口的函数指针,但是我收到了这个错误。

error: cannot convert boost::function to char* ( )(void , Tcl_Interp*, const char*, const char*, int) for argument 4 to int Tcl_TraceVar(Tcl_Interp*, const char*, int, char* ( )(void , Tcl_Interp*, const char*, const char*, int), void*) 错误:无法将boost :: function转换为char *( )(void ,Tcl_Interp *,const char *,const char *,int),参数4为int Tcl_TraceVar(Tcl_Interp *,const char *,int,char *( )( void ,Tcl_Interp *,const char *,const char *,int),void *)

I think something is wrong with binding part. 我认为绑定部分有问题。 Can you please correct it? 你能纠正吗?

The error tells you exactly what's wrong: you can't convert boost::function to a plain function pointer. 该错误告诉您到底出了什么问题:您无法将boost::function转换为普通函数指针。 You'll have to write a non-member function and pass a pointer to that; 你必须写一个非成员函数并传递一个指向它的指针; it can call a member function on an object if you pass a pointer to that object as the client data. 如果将指向该对象的指针作为客户端数据传递,它可以调用对象上的成员函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM