Created
December 4, 2019 13:43
-
-
Save croraf/6284d93da03f19fe70a396b7caeba042 to your computer and use it in GitHub Desktop.
C OO pattern
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
``` | |
struct MyClass; | |
MyClass* MyClassConstructor(int i); | |
void destroyMyClass(MyClass*); | |
void incrementMyClass(struct MyClass*); | |
``` | |
``` | |
#include "./MyClass.h" | |
static int main(){ | |
MyClass* instance = MyClassConstructor(5); | |
incrementMyClass(instance); | |
destroyMyClass(instance); | |
} | |
``` | |
``` | |
struct MyClass | |
{ | |
int i; | |
}; | |
MyClass* MyClassConstructor(int i) | |
{ | |
MyClass* obj = malloc(sizeof(MyClass)); | |
obj->i = i; | |
return obj; | |
} | |
void destroyMyClass(MyClass* obj) | |
{ | |
free(obj); | |
} | |
void incrementMyClass(struct MyClass* obj) | |
{ | |
obj->i++; | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment