简体   繁体   中英

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.

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*)

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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