cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How to avoid static function in c++ when calling CURLOPT_WRITEFUNCTION

From: tetetest tetetest <tetetest_at_rambler.ru>
Date: Wed, 24 Sep 2008 11:28:38 +0400

* Meir Yanovich <meiry242_at_gmail.com> [Wed, 24 Sep 2008 09:34:14 +0300]:
> Hello all
> im using curl in my application and its c++ application
> im encapsulate the curl init in class and i like to avoid using static
> member function
> for better multi threaded and OOP support .
> but i have problem when i pass the call back function into
> CURLOPT_WRITEFUNCTION
> it accepts only static function , is there away to pass it not static
> call back function ?

The short answer is no: CURL is a C library, so it doesn't know anything
about C++ member functions.

Yet, you can overcome this limitation with a relative ease using a
static member function that is passed a pointer to the class:

// f is the pointer to your object.
static YourClass::staticFunction(void *buffer, size_t size, size_t
nmemb, void *f)
{
   // Call non-static member function.
   static_cast<YourClass*>(f)->nonStaticFunction();
}

// This is how you pass pointer to the static function:

        curl_easy_setopt(hcurl, CURLOPT_WRITEFUNCTION,
YourClass:staticFunction);
        curl_easy_setopt(hcurl, CURLOPT_WRITEDATA, this);

--
tetetest tetetest.
Received on 2008-09-24