include string include memory include stdlib include stdio define new

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#define new(class) class##_constructor((struct class*)memset(malloc(sizeof(struct class)), 0, sizeof(struct class)))
#define set_method(C, O, M) O->M = C##_##M
#define get_object(O) O
#define method(O, M) O->M(O
#define args(...) ,__VA_ARGS__)
#define noargs() )
struct A
{
int field;
void (*setField)(struct A*, int);
void (*printField)(struct A*);
};
void A_printField(struct A* self)
{
printf("%d\n", self->field);
}
void A_setField(struct A* self, int value)
{
self->field = value;
};
struct A* A_constructor(struct A* self)
{
set_method(A, self, setField);
set_method(A, self, printField);
return self;
}
int main()
{
struct A* objectA = new(A);
method(objectA, setField) args(10);
method(objectA, printField) noargs();
int a = 50;
unsigned char b = a;
printf("%d\n", b);
}