将类方法注册为C样式回调 [英] registering class methods as C style callbacks
问题描述
考虑以下问题:
您有一个C样式库和API,它使用回调来实现
功能。这方面的例子是X11 API,OpenGL / GLUT ......列表打开了
。
C ++中的虚拟功能导致我们想要实现一个框架
,其中这些回调只是在派生的
类中覆盖虚拟方法。所以...
struct A {
A(){:: set_timerroutine(timerroutine); }
~A(){:: clear_timerroutine(); } $ / $
void timerroutine(){action(); }
虚拟空行动()= 0;
};
结构B:公共A {
B():A(){}
action(){//在此处执行相关操作}
};
B myTimerObject;
表面上我们希望在A的构造函数中注册类方法
timerroutine将导致B :: action()为<调用了
,但我没有找到一种方法将类方法强制转换为通用的
函数指针,就像在C风格的回调中所期望的那样。
>
如何在没有极其难看的开销的情况下实现这一点
"引用C样式timerroutine()中的静态对象指针
本身不是a类方法?"
想法/要求是有一个框架,它只能存在于一个
头文件中,因此可以作为模板。
-Rob
consider the following problem:
You have a C style library and API that uses callbacks to implement
functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes
on.
The power of virtuals in C++ leads us to want to implement a framework
where those callbacks are simply overriden virtual methods in a derived
class. So...
struct A {
A() { ::set_timerroutine(timerroutine); }
~A() { ::clear_timerroutine(); }
void timerroutine() { action(); }
virtual void action()=0;
};
struct B: public A {
B(): A() {}
action() { // do something relevant here }
};
B myTimerObject;
On the surface we would expect that registering the class method
timerroutine in the constructor for A would cause B::action() to be
invoked but I''ve not found a way to cast a class method to a generic
function pointer as is expected in C style callbacks.
How can I accomplish this without the extremely ugly overhead of
"referencing static object pointers in a C style timerroutine() that is
not itself a class method?"
The idea/requirement is to have a framework that can exist solely in a
header file and thus serve as a template.
-Rob
推荐答案
无*** @所有。 com 写道:
no***@all.com wrote:
表面上我们希望在A的构造函数中注册类方法
timerroutine会导致调用B :: action()
但是我没有找到一种方法将类方法转换为泛型
函数指针,如C风格所期望的那样回调。
On the surface we would expect that registering the class method
timerroutine in the constructor for A would cause B::action() to be
invoked but I''ve not found a way to cast a class method to a generic
function pointer as is expected in C style callbacks.
施法是不够的。要使用成员函数,您需要函数
和一个对象。
带回调的系统通常提供一种将指针传递给
函数设置回调,并在调用
回调函数时传递此指针值。你只需要使用指向基类的指针,然后用
来调用你想要的虚函数。
-
Salu2
A cast is not enough. To use a member function you need both the function
and an object.
Systems with callbacks usually provide a way to pass a pointer to the
function that sets the callback, and pass this pointer value when calling
the callback function. You just need to use a pointer to a base class, and
use it to call the virtual function you want.
--
Salu2
2006年7月17日星期一21:22:00 +0200,JuliánAlbo写道:
On Mon, 17 Jul 2006 21:22:00 +0200, Julián Albo wrote:
< a href =mailto:no *** @ all.com> no *** @ all.com 写道:
no***@all.com wrote:
>关于表面我们希望在A的构造函数中注册类方法
timerroutine会导致调用B :: action()但是我没有找到将类方法强制转换为a的方法通用
函数指针在C样式回调中是预期的。
>On the surface we would expect that registering the class method
timerroutine in the constructor for A would cause B::action() to be
invoked but I''ve not found a way to cast a class method to a generic
function pointer as is expected in C style callbacks.
施法是不够的。要使用成员函数,您需要函数
和一个对象。
带回调的系统通常提供一种将指针传递给
函数设置回调,并在调用
回调函数时传递此指针值。你只需要使用一个指向基类的指针,然后用
来调用你想要的虚函数。
A cast is not enough. To use a member function you need both the function
and an object.
Systems with callbacks usually provide a way to pass a pointer to the
function that sets the callback, and pass this pointer value when calling
the callback function. You just need to use a pointer to a base class, and
use it to call the virtual function you want.
要么我不理解你的解释,要么你完全理解我的完全问题:或许两者兼而有之。注册一个回调只是使用了一个
函数指针(ptr *)(),因为类方法不是真正的函数
指针它们不能直接在使用C风格的API中注册
回调。我需要一种注册它们的方法,它不需要静态的指针,或全局C函数来绕过函数指针
问题。这个解决方案必须完全可以在头文件中实现,所以
它可以作为模板。
Either I''m not understanding your explanation or you don''t understand my
problem fully: maybe a bit of both. registering a callback simply uses a
function pointer (ptr*)() and since class methods are not really function
pointers they cannot be directly registered in APIs that use C style
callbacks. I need a way of registering them that does NOT require static
pointers, or global C functions to get around the function pointer
problem. this solution MUST be fully implementable in a header file so
that it can serve as a template.
no *** @ all.com 写道:
no***@all.com wrote:
或许我不理解你的解释或你完全不理解我的完全问题:或许两者兼而有之。注册一个回调只是使用了一个
函数指针(ptr *)(),因为类方法不是真正的函数
指针它们不能直接在使用C风格的API中注册
回调。我需要一种注册它们的方法,它不需要静态的指针,或全局C函数来绕过函数指针
问题。这个解决方案必须完全可以在头文件中实现,所以
它可以作为模板。
Either I''m not understanding your explanation or you don''t understand my
problem fully: maybe a bit of both. registering a callback simply uses a
function pointer (ptr*)() and since class methods are not really function
pointers they cannot be directly registered in APIs that use C style
callbacks. I need a way of registering them that does NOT require static
pointers, or global C functions to get around the function pointer
problem. this solution MUST be fully implementable in a header file so
that it can serve as a template.
你想要它的事实并不能实现。如果你想使用
非静态成员函数,你需要一种方法来提供一个对象。如果
回调样式不允许,你需要使用全局变量或一些
其他解决方法。
- -
Salu2
The fact that you want it does not make it possible. If you want to use a
non static member function you need a way to provide an object. If the
callback style does not allow it, you need to use a global variable or some
other workaround.
--
Salu2
这篇关于将类方法注册为C样式回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!