Advertisement
sebbu

OOP in C

Oct 6th, 2019
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct MaClasse;
  5.  
  6. typedef struct MaClasse
  7. {
  8.     void (*init)(struct MaClasse** this);
  9.     void (*free)(struct MaClasse** this);
  10.     void (*print)(void);
  11. } MaClasse;
  12.  
  13. void MaClasse_init(struct MaClasse** this);
  14. void MaClasse_free(struct MaClasse** this);
  15. void MaClasse_print();
  16.  
  17. void MaClasse_init(struct MaClasse** this)
  18. {
  19.     *this = (MaClasse*) calloc(1, sizeof(MaClasse));
  20.     (*this)->init = &MaClasse_init;
  21.     (*this)->free = &MaClasse_free;
  22.     (*this)->print = &MaClasse_print;
  23. }
  24.  
  25. void MaClasse_free(struct MaClasse** this)
  26. {
  27.     free(*this);
  28.     *this = NULL;
  29. }
  30.  
  31. void MaClasse_print()
  32. {
  33.     printf("Hello World!\n");
  34. }
  35.  
  36. int main(void)
  37. {
  38.     MaClasse *a;
  39.     MaClasse_init(&a);
  40.     a->print();
  41.     a->free(&a);
  42.     return EXIT_SUCCESS;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement