转换C ++函数指针到c函数指针 [英] Convert C++ function pointer to c function pointer
问题描述
我开发使用C库中的C ++应用程序。我要送一个函数指针的C库。
I am developing a C++ application using a C library. I have to send a pointer to function to the C library.
这是我的类:
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
Ui::MainWindow *ui;
void f(int*);
private slots:
void on_btn_clicked();
};
这是我的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);
}
C函数应该获得一个指向这样一个函数:void F(INT *)。
但上面的code不工作,我能不能成功,以我的F成员函数转换为所需的指针。
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.
任何人都可以请帮助?
推荐答案
如果我记错了,只有类的静态方法可以通过正常的C指针访问到函数的语法。所以,尽量做到静态的。该指针指向一个类的方法需要额外的信息,比如它有一个纯C的方法没有任何意义对象(本)。
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.
这篇关于转换C ++函数指针到c函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!