This is the second Nekuromento example factorized in a way that I find idiomatic for object-oriented C:
animal.h
#ifndef ANIMAL_H_ #define ANIMAL_H_ struct animal {
cat.c
#include "animal.h" static const char *sound(void) { return "meow!"; } const struct animal_vtable_ CAT[] = { { sound } };
dog.c
#include "animal.h" static const char *sound(void) { return "arf!"; } const struct animal_vtable_ DOG[] = { { sound } };
main.c
#include "animal.h" #include <stdio.h> int main(void) { struct animal kitty = { CAT, "Kitty" }; struct animal lassie = { DOG, "Lassie" }; printf("%s says %s\n", kitty.name, animal_sound(&kitty)); printf("%s says %s\n", lassie.name, animal_sound(&lassie)); return 0; }
This is an example of run-time polymorphism that occurs when a method resolves.
C1x added a generic choice that makes macro-compilation time polymorphism possible. The following example is taken from the C1x April project, section 6.5.1.1 §5:
#define cbrt(X) _Generic((X), \ long double: cbrtl, \ default: cbrt, \ float: cbrtf \ )(X)
Type-based macros for mathematical functions were already available on C99 through the tgmath.h header, but users were unable to define their own macros without using compiler extensions.
Christoph
source share