#define _GNU_SOURCE #include "Dog.h" #include #include #include // why is this function not static? void _delete(Dog d) { free(d->name); free(d); } // why is this function not static? void _bark(Dog d) { printf("%s says Woof!\n", d->name); } static char *_toString(Dog d) { char *resultStr; asprintf(&resultStr, "Dog(%s, %d)", d->name, d->age); return resultStr; } static struct dog_vtable_st DOG_VTABLE = { .delete = _delete, .bark = _bark, .toString = _toString }; int _dog_constructor(Dog d, char *name, int age) { size_t namelen = strlen(name); d->name = (char *)malloc(sizeof(namelen + 1)); if (!d->name) return 0; strcpy(d->name, name); d->age = age; return 1; } Dog new_dog(char *name, int age) { Dog dog = (Dog)malloc(sizeof(struct dog_st)); if (!dog) return NULL; dog->vtable = &DOG_VTABLE; if (!_dog_constructor(dog, name, age)) { free(dog); return NULL; } return dog; }