C function pointer callback as struct member with "self" reference parameter -


this question has answer here:


want create task struct containing function pointer callback execute said task. task contains parameters pass "this/self" pointer of struct callback executor function. creates circular dependencies , i've been digging around trying various forward declarations etc can't seem right. missing makes impossible, or c syntax wizardry horribly weak. changing task* void* seems cheating?

in task.h :

// create function pointer type signature executing task typedef int (*executor) (task* self);  // create task type typedef struct {     executor exec;  // callback execute task     ... // various data task } task; 

forward declare struct task, declare function pointer using struct task, declare struct task , typedef task.

struct task; typedef int (*executor) (struct task* self);  typedef struct task {     executor exec;  // callback execute task     ... // various data task } task; 

or jens suggested:

first typedef task forward declaration of struct task, declare function pointer (using typedef task) , struct task.

typedef struct task task; typedef int (*executor) (task* self);  struct task {     executor exec;  // callback execute task     ... // various data task }; 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -