相关疑难解决方法(0)

如何将成员函数传递给函数指针?

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

10
推荐指数
1
解决办法
5695
查看次数

lambda 到 curl 回调函数

我正在尝试将 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

我在这里粘贴了完整的示例。

c++ c++11

3
推荐指数
1
解决办法
1151
查看次数