GTK如何和C++连接作界面
GTK怎么和C++连接作界面?
GTK+ Tutorial中讲到了一种方法,但是我看不懂,有没有哪位大神会用,给个简单的例子介绍一下:
原文如下:
Second, you can use GTK and C++ together by declaring all callbacks as static functions in C++ classes, and again calling GTK using its C interface. If you choose this last approach, you can include as the callback's data value a pointer to the object to be manipulated (the so-called "this" value).
------解决方案--------------------
GTK的话用C语言比较方便,GTKMM是它的C++绑定,所以你用C++的话它会相当方便。
------解决方案--------------------
说的是 gtk 有些接受回调函数的接口,这种回调函数必须为普通的 c 函数指针,这就给使用 c++ 类成员函数造成了一个问题,因为 c++ 规定类型成员函数指针不能转换为普通的函数指针。gtk 说这种情况下一个标准的解决方案就是,使用 c++ 类静态函数,这种函数能够退化为函数指针,但这就带来了被调用的 c++ 对象信息丢失的问题,于是可以把 c++ 静态函数改成接受 void* data 的接口,然后调用的时候把对象的地址,即 this 指针,传递为回调函数的 data 参数,然后在和回调函数中强制将 data 转换为 this 指针即可。比如
GTK+ Tutorial中讲到了一种方法,但是我看不懂,有没有哪位大神会用,给个简单的例子介绍一下:
原文如下:
Second, you can use GTK and C++ together by declaring all callbacks as static functions in C++ classes, and again calling GTK using its C interface. If you choose this last approach, you can include as the callback's data value a pointer to the object to be manipulated (the so-called "this" value).
------解决方案--------------------
GTK的话用C语言比较方便,GTKMM是它的C++绑定,所以你用C++的话它会相当方便。
------解决方案--------------------
说的是 gtk 有些接受回调函数的接口,这种回调函数必须为普通的 c 函数指针,这就给使用 c++ 类成员函数造成了一个问题,因为 c++ 规定类型成员函数指针不能转换为普通的函数指针。gtk 说这种情况下一个标准的解决方案就是,使用 c++ 类静态函数,这种函数能够退化为函数指针,但这就带来了被调用的 c++ 对象信息丢失的问题,于是可以把 c++ 静态函数改成接受 void* data 的接口,然后调用的时候把对象的地址,即 this 指针,传递为回调函数的 data 参数,然后在和回调函数中强制将 data 转换为 this 指针即可。比如
struct test_t
{
void member_func () {}
static void static_func (void* data)
{
test_t* me = reinterpret_cast<test_t*>(data);
me->member_func();
}
};
int main ()
{
test_t t;
some_gtk_callback_register(test_t::static_func,&t);
}