Skip to content

Instantly share code, notes, and snippets.

@keyboard-slayer
Created August 20, 2022 22:31
Show Gist options
  • Save keyboard-slayer/7b1e75b69062af1c42fb0f9c13ad98be to your computer and use it in GitHub Desktop.
Save keyboard-slayer/7b1e75b69062af1c42fb0f9c13ad98be to your computer and use it in GitHub Desktop.
Cringe Oop
#include <stdlib.h>
#include <assert.h>
#define CLASS(class$_name, class$_fields, class$_constr_args, class$_constr_body) \
typedef struct class$_name \
class$_fields \
class$_name; \
class$_name *class$_name ## _init class$_constr_args \
{ \
class$_name *self = malloc(sizeof(class$_name)); \
class$_constr_body \
return self; \
} \
#define new(TYPE, ...) TYPE ## _init(__VA_ARGS__);
CLASS(IntClass, {int n;}, (int n), {self->n = n;})
int main(void)
{
IntClass *i = new(IntClass, 42);
assert(i->n == 42);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment