C callback table example
This commit is contained in:
parent
0549f55bfa
commit
746261e4a7
1 changed files with 31 additions and 0 deletions
31
callbacks.c
Normal file
31
callbacks.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Callback table, used for abstracting behaviour. Also known as function
|
||||
* pointer table This is whats caleld a vtable in C++ terms.
|
||||
*
|
||||
* Commonly used as a form of polymorphism
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static int global = 0;
|
||||
|
||||
typedef struct {
|
||||
void (*put)(int);
|
||||
int (*get)();
|
||||
} Test;
|
||||
|
||||
void my_put(int a) { global = a; }
|
||||
int my_get() { return global; }
|
||||
|
||||
void executor(Test *t) {
|
||||
t->put(10);
|
||||
int got = t->get();
|
||||
|
||||
printf("Got: %d\n", got);
|
||||
}
|
||||
|
||||
int main() {
|
||||
Test t = {.get = my_get, .put = my_put};
|
||||
executor(&t);
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue