RRR RRR - 6 months ago 44
C Question

Convert C++ function pointer to c function pointer

I am developing a C++ application using a C library. I have to send a pointer to function to the C library.

This is my class:

class MainWindow : public QMainWindow {
explicit MainWindow(QWidget *parent = 0);
Ui::MainWindow *ui;
void f(int*);

private slots:
void on_btn_clicked();

This is my on_btn_clicked function:

void MainWindow::on_btn_clicked()
void (MainWindow::* ptfptr) (int*) = &MainWindow::f;

c_library_function(static_cast<void()(int*)>(ptfptr), NULL);


The C function should get a pointer to a such function : void f(int*).
But the code above doesn't work, I cannot succeed to convert my f member function to the desired pointer.

Can anybody please help?


If I recall it correctly, Only static methods of a class can be accessed via "normal" C pointer to function syntax. So try to make it static. The pointer to a method of a class needs extra information, such as the "object" (this) which has no meaning for a pure C method.

The FAQ shown here has good explanation and a possible (ugly) solution for your problem.