class Child;
class Parent
{
public:
void (*funcPointer)();
void (*funcPointer2)(Parent* _this);
void (Child::*funcPointer3)();
};
class Child: public Parent
{
public:
void TestFunc(){
}
void Do(){
Parent p;
p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)'
p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)'
p.funcPointer3=TestFunc; //this works
p.funcPointer3=&Child::TestFunc; // this works too.
p.funcPointer3(); // error, term does not evaluate to a function taking 0 arguments …Run Code Online (Sandbox Code Playgroud) c++ methods member-function-pointers function-pointers delayed-execution
我正在尝试将 lambda 传递给CURLOPT_WRITEFUNCTION.
该函数需要一个静态函数,但是,我从这个问题中了解到lambda 将被隐式转换,并且我可以从 lambda 调用成员函数。
auto callback = [](char * ptr_data, size_t size, size_t nmemb, string * writerData)
->size_t
{
if(writerData == NULL)
return 0;
size_t data_size = size * nmemb;
writerData->append(ptr_data, data_size);
return (int)data_size;
};
CURLcode code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, callback);
Run Code Online (Sandbox Code Playgroud)
这实际上编译,但卷曲段错误: Segmentation fault: 11
我在这里粘贴了完整的示例。