如何在libcurl中使用成员函数指针 [英] How can I use a member function pointer in libcurl

查看:147
本文介绍了如何在libcurl中使用成员函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用libcurl我有我下载的文件里面的类,我想看到一个进度功能。我注意到我可以设置一个典型的函数指针

I am using libcurl I have my downloading of files inside of a class, to which I want to see a progress function. I notice I can set a typical function pointer by

curl_easy_setopt(mCurl, CURLOPT_PROGRESSFUNCTION, progress_func3);

但是,我想将它设置为指向我类的函数指针。我可以得到代码编译与

However, I would like to set it to a function pointer to my class. I can get the code to compile with

curl_easy_setopt(mCurl, CURLOPT_PROGRESSFUNCTION, &MyClass::progress_func3);

并且 progress_func3 函数将被调用。问题是,一旦返回就会有一个缓冲区溢出检测!错误通过,说程序不能安全地继续和执行,必须终止。 (这是一个Microsoft Visual C ++运行库错误窗口,我使用Visual Studio 2010)。

and the progress_func3 function will get called. The problem is, as soon as it returns there will be a "Buffer overrun detected!" error through, saying the program can not safely continue and execute, and must be terminated. (It is a Microsoft Visual C++ Runtime Library error window, I am using Visual Studio 2010).

当我使用一个函数,没有问题,但是当我使用一个成员函数指针,我会得到这个错误。如何在libcurl中使用成员函数指针?

When I use a function, there is no problem, but when I use a member function pointer, I will get this error. How can I use a member function pointer in libcurl?

推荐答案

非静态成员函数需要这个指针是可调用的。您无法使用此类型的接口提供指针,因此无法使用非静态成员函数。

A non-static member function needs a this pointer to be callable. You can't provide that this pointer with this type of interface, so using a non-static member function is not possible.

您应该创建一个plain C函数作为回调函数,并在相应的 MyClass 实例上调用该函数的成员函数。

You should create a "plain C" function as your callback, and have that function call the member function on the appropriate MyClass instance.

尝试类似:

int my_progress_func(void *clientp, ...)
{
   MyClass *mc = static_cast<MyClass*>(clientp);
   mc->your_function(...);
   return 0; // or something else
}

然后:

curl_easy_setopt(mCurl, CURLOPT_PROGRESSDATA,     &your_myclass_object);
curl_easy_setopt(mCurl, CURLOPT_PROGRESSFUNCTION, my_progress_func);

(你必须负责这里的类型匹配, code> MyClass 指向进度数据的指针,你是自己的。)

(You're responsible for the type match here, obviously. If you attach anything else but a MyClass pointer to the progress data, you're on your own.)

这篇关于如何在libcurl中使用成员函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆