Skip to content

Instantly share code, notes, and snippets.

@wilkie
Forked from duckinator/new.c
Created August 19, 2010 03:31
Show Gist options
  • Save wilkie/536945 to your computer and use it in GitHub Desktop.
Save wilkie/536945 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#define new(TYPE, ARGS...) ({\
TYPE *item = ( TYPE *)malloc(sizeof( TYPE )); \
item->init = &TYPE##Init; \
item->init(item, ARGS); \
item; \
})
typedef struct {
int scancode;
int shift;
int control;
// constructor
void(*init)(void*, int, int, int);
} KeyEvent;
// Constructor
void KeyEventInit(void* st, int scancode, int shift, int control) {
KeyEvent* ke = (KeyEvent*)st;
ke->scancode = scancode;
ke->shift = shift;
ke->control = control;
}
int main() {
KeyEvent* a = new(KeyEvent, 1, 2, 3);
printf("scancode = %i, shift = %i, control = %i\n", a->scancode, a->shift, a->control);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment