I am using curl library which returns data to me through a callback function with the prototype below
size_t write_data(void * data, size_t size, size_t nmemb, void * userpointer);
I noticed that if i declare a function fitting this prototype in my class
//file Dialog.h
class Dialog : public QDialog
{
private:
int new_data_callback(void * newdata, size_t size, size_t nmemb, QByteArray * buffer);
}
If i try to use it in my Dialog.cpp
curl_easy_setopt(curl_handle,CURLOPT_WRITEFUNCTIION, new_data_callback);
I get an error
Invaid use of member (did you forget the '&'?)
If i add static to my function declaration, it compiles.
static int new_data_callback(void * newdata, size_t size, size_t nmemb, QByteArray * buffer); //ok
Question
Why is static needed in this case?
PS: the classes beginning with Q eg QDialog are part of QT and dont affect the question.
Because a non-static method cannot be called without an instance. Since
new_data_callbackis a callback, the only way to attach an instance to it is through a parameter. Making itstaticremoves the instance restriction.