callback pointing to a different function

  • Thread starter christophe.verne
  • Start date
C

christophe.verne

Hi all,
It's the first time I'm using groups so i hope I am posting on the good
group.

I''m not familiar with callback and function pointer and here is what
I'd like to do:

typedef struct Control{
TYPE Type;
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
void *callback;
};

with callback a pointer to a function. Control will be used in 2 cases,
and depending on the case, it will call one function or the other. As
the arguments are not the same, how can I declare it? I was thinking of
doing the following:
typedef (* case1) (int arg1, int arg2, float arg3);
typedef (* case2 )(int arg1, float arg2, char arg3);

Control panel;
Control menu;
new_(panel, PANEL_TYPE)
new_(menu,MENU_TYPE)
where new_ initialize panel and menu, making the callback pointing to
the good function...

In one case, I would call (case1) (panel.callback)(int1,int2,float3);
in the other case, I would call (case2)(menu.callback)(int1, float2,
char3)

Is this could work, or I have not understood how it works :( . I've
been looking for a tutorial on callbacks, but I haven't found something
for my problem, where a callback could be assigned to 2 differents
functions with differents arguments.

Thanks for the help.
ctitof
 
B

bert

Hi all,
It's the first time I'm using groups so i hope I am posting on the good
group.

I''m not familiar with callback and function pointer and here is what
I'd like to do:

typedef struct Control{
TYPE Type;
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
void *callback;
};

with callback a pointer to a function. Control will be used in 2 cases,
and depending on the case, it will call one function or the other. As
the arguments are not the same, how can I declare it? I was thinking of
doing the following:
typedef (* case1) (int arg1, int arg2, float arg3);
typedef (* case2 )(int arg1, float arg2, char arg3);

Control panel;
Control menu;
new_(panel, PANEL_TYPE)
new_(menu,MENU_TYPE)
where new_ initialize panel and menu, making the callback pointing to
the good function...

In one case, I would call (case1) (panel.callback)(int1,int2,float3);
in the other case, I would call (case2)(menu.callback)(int1, float2,
char3)

Is this could work, or I have not understood how it works :( . I've
been looking for a tutorial on callbacks, but I haven't found something
for my problem, where a callback could be assigned to 2 differents
functions with differents arguments.

Thanks for the help.
ctitof

The argument lists would be the same if the logical arguments were
packed into a single structure, but different for each function, and
their actual argument was a void pointer which each function cast
to the appropriate type of struct pointer. But the possibilities of
mistakes in use are then greater.
--
 
F

Flash Gordon

Hi all,
It's the first time I'm using groups so i hope I am posting on the good
group.

I''m not familiar with callback and function pointer and here is what
I'd like to do:

typedef struct Control{
TYPE Type;
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
void *callback;
};

with callback a pointer to a function.

void* is not suitable for storing a function pointer.
> Control will be used in 2 cases,
and depending on the case, it will call one function or the other. As
the arguments are not the same, how can I declare it? I was thinking of
doing the following:
typedef (* case1) (int arg1, int arg2, float arg3);
typedef (* case2 )(int arg1, float arg2, char arg3);

You have not specified what return type these functions have.

<snip>

Use one function pointer type in the struct, and cast it to the correct
function pointer type before calling it. E.g.

typedef void (dummy_func_type)(void)

typedef struct Control{
TYPE Type;
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
dummy_func_type *callback;
};

typedef void (case1_func_type)(int arg1, int arg2, float arg3);
typedef void (case2_func_type)(int arg1, float arg2, char arg3);

switch (foo.Type) {
case 1: ((case1_func_type*)foo.callback)(i1,i2,f);
break;
case 2: ((case2_func_type*)foo.callback)(i,f,c);
break;

and so on.

Note that I have defined a type for the function, not a pointer to a
function. I find this clearer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,833
Latest member
BettyeMacf

Latest Threads

Top