Last active
November 3, 2019 12:04
-
-
Save Silva97/cc2a9ab7777755de25be65e82636fa85 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
| struct example { | |
| void (*add)(struct example *, int); | |
| void (*show)(struct example *); | |
| int number; | |
| }; | |
| void example_add(struct example *this, int n) | |
| { | |
| this->number += n; | |
| } | |
| void example_show(struct example *this) | |
| { | |
| printf("this->number = %d\n", this->number); | |
| } | |
| struct example *new_example(int n) | |
| { | |
| struct example *new = malloc( sizeof (struct example) ); | |
| new->add = example_add; | |
| new->show = example_show; | |
| new->number = n; | |
| return new; | |
| } | |
| void destroy_example(struct example *obj) | |
| { | |
| free(obj); | |
| } | |
| int main(void) | |
| { | |
| struct example *test = new_example(5); | |
| test->add(test, 4); | |
| test->show(test); | |
| destroy_example(test); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment