Last active
January 27, 2022 00:10
-
-
Save Mroik/4abc8ad1788555e4a00d06ab87494af7 to your computer and use it in GitHub Desktop.
This is an example of how to program with OOP paradigm in C (why would you ever do this)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef int(*int_ptr)(test); | |
typedef struct { | |
int a; | |
int_ptr next; | |
} test; | |
int next_r(test* self) | |
{ | |
return self->a + 1; | |
} | |
test* test_maker() | |
{ | |
test* a = malloc(sizeof(test)); | |
a->next = &next_r; | |
return a; | |
} | |
int main() | |
{ | |
test* ss = test_maker(); | |
ss->a = 1; | |
printf("%d\n", ss->next(ss)); // Prints 2 | |
free(ss); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment