Skip to content

Instantly share code, notes, and snippets.

@hsk
Created July 5, 2014 00:53
Show Gist options
  • Save hsk/46fd1f1f46bfb58c6b60 to your computer and use it in GitHub Desktop.
Save hsk/46fd1f1f46bfb58c6b60 to your computer and use it in GitHub Desktop.
#include <stdio.h>
extern "C" {
void printi(int i) {
printf("%d\n", i);
}
typedef void (*A_p)(struct A*);
struct A {
static void _new(struct A* self) {
static char* vtable[] = {
(char*)A::_p,
};
self->p=(A_p*)&vtable[0];
}
A_p* p;
static void _p(struct A* self) {
printi(1);
};
};
typedef void (*B_p)(struct B*);
struct B {
static void _new(struct B* self) {
static char* vtable[] = {
(char*)B::_p2,
};
self->p2=(B_p*)&vtable[0];
}
B_p* p2;
static void _p2(struct B* self) {
printi(2);
}
};
struct C {
struct A a;
struct B b;
static void _p2(struct B* self) {
printi(33);
}
static void _new(struct C* self) {
static char* vtable[] = {
(char*)A::_p,
(char*)_p2,
};
A::_new(&self->a);
B::_new(&self->b);
self->a.p = (A_p*)&vtable[0];
self->b.p2 = (B_p*)&vtable[1];
}
};
int main() {
struct C c;
C::_new(&c);
(*c.a.p)(&c.a);
(*c.b.p2)(&c.b);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment