How can I use a member function pointer in libcurl

c++, curl, function-pointers, libcurl, pointer-to-member

Solution

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.

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

Try something like:

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

Then:

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

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

Problem

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); ``` 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). 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?

Original source