在给定的回调中需要addititional arg [英] need addititional arg in given callback

查看:111
本文介绍了在给定的回调中需要addititional arg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



给定库中的
我用这个函数注册回调函数:


bool set_callback(int index,int(* callback_function)(long));


我需要回调函数来传递索引,但是,

没有(重新)通过回调函数。


通常情况下,如果设计中已知回调函数的数量,我只需为每个

索引注册回调函数

时间......但它不是。


有人能帮我找到解决问题的正确方法吗?


Felix

Hi,

in a given library I register callback functions with this function:

bool set_callback(int index, int (*callback_function)(long));

I need the callback function to also pass the index which is, however,
not (re-)passed by the callback function.

Normally, I would just register callback functions for each
index I need if the amount of callback functions was known at design
time... But it isn''t.

Could someone help me find the right approach to solve that problem?

Felix

推荐答案

Felix Kater< f。****** @ gmx.net>在给定的库中写道:
Felix Kater <f.******@gmx.net> wrote:
我用这个函数注册了回调函数:

bool set_callback(int index,int(* callback_function)(long));

我需要回调函数来传递索引,但是,回调函数没有(重新)传递索引。

通常情况下,我会如果在设计时已知回调函数的数量,只需为每个
索引注册回调函数......但它不是。

有人可以帮助我找到解决问题的正确方法?
in a given library I register callback functions with this function:

bool set_callback(int index, int (*callback_function)(long));

I need the callback function to also pass the index which is, however,
not (re-)passed by the callback function.

Normally, I would just register callback functions for each
index I need if the amount of callback functions was known at design
time... But it isn''t.

Could someone help me find the right approach to solve that problem?




这是一个窘境。你想要关闭类似这样的东西,但是你不需要那些在C中的人。快速而肮脏的黑客就是使用

文件 - 范围(即全局,或者如果可能的话,文件本地)变量,

可能借助辅助函数,如果你不能改变

回调函数本身。


Richard



It''s a quandary. You''d like closures for something like this, but you
don''t have those in C. A quick and dirty hack would be to use a
file-scope (i.e., global, or if at all possible, file-local) variable,
possibly with the aid of a helper function if you can''t change the
callback function itself.

Richard


rl *@hoekstra-uitgeverij.nl (Richard Bos)写道:
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
快速而肮脏的黑客将是使用
文件范围(即全局)或者,如果可能的话,文件本地)变量,
可能借助辅助函数
A quick and dirty hack would be to use a
file-scope (i.e., global, or if at all possible, file-local) variable,
possibly with the aid of a helper function




很高兴听到有这样的方法但是,你能否详细说明一下

对我来说?


Felix



Good to hear that there''s a way, however, could you elaborate that a
bit to me?

Felix


Felix Kater <˚F****** @ gmx.net>写道:给定库中的
Felix Kater <f.******@gmx.net> writes:
我用这个函数注册回调函数:

bool set_callback(int index,int(* callback_function)(long));

我需要回调函数来传递索引,然而,回调函数没有(重新)传递索引。

通常,我只会注册回调函数如果回调函数的数量在设计时已知,我需要的每个
索引......但它不是。
in a given library I register callback functions with this function:

bool set_callback(int index, int (*callback_function)(long));

I need the callback function to also pass the index which is, however,
not (re-)passed by the callback function.

Normally, I would just register callback functions for each
index I need if the amount of callback functions was known at design
time... But it isn''t.




如果你不能改变传递给回调

函数的参数的类型,你可以使它的参数成为数据结构的索引

它可以从中检索你需要提供尽可能多的信息。


最简单的数据结构当然是一个数组(大概是
结构或指向结构的指针)。如果您事先不知道

您需要多少元素,请动态分配并根据需要使用

realloc()来扩展它。 />

-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



If you can''t change the type of the argument passed to the callback
function, you could make its argument an index into a data structure
from which it can retrieve as much information as you care to provide.

The simplest such data structure, of course, is an array (presumably
of structs or of pointers to structs). If you don''t know in advance
how many elements you''ll need, allocate it dynamically and use
realloc() as needed to expand it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于在给定的回调中需要addititional arg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆