[ create a new paste ] login | about

Link: http://codepad.org/zDkd4LZD    [ raw code | output | fork | 1 comment ]

RogerPate - C, pasted on Feb 27:
void B_foo(void) { puts(__func__); }
void D_foo(void) { puts(__func__); }

struct B_VT {
  void (*foo)(void);
}
B_vtable = { B_foo },
D_vtable = { D_foo };

typedef struct B {
  struct B_VT* vt;
} B;
B* new_B(void) {
  B* p = malloc(sizeof(B));
  p->vt = &B_vtable;
  return p;
}

typedef struct D {
  struct B_VT* vt;
} D;
D* new_D(void) {
  D* p = malloc(sizeof(D));
  p->vt = &D_vtable;
  return p;
}

int main() {
  B* a[] = {new_B(), new_D()};
  a[0]->vt->foo();
  a[1]->vt->foo();
  return 0;
}


Output:
1
2
B_foo
D_foo


Create a new paste based on this one


Comments:
posted by Kniht on Mar 11
reply