Skip to content

Instantly share code, notes, and snippets.

@Silva97
Last active November 3, 2019 12:04
Show Gist options
  • Select an option

  • Save Silva97/cc2a9ab7777755de25be65e82636fa85 to your computer and use it in GitHub Desktop.

Select an option

Save Silva97/cc2a9ab7777755de25be65e82636fa85 to your computer and use it in GitHub Desktop.
#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