This is a creation in Article, where the information may have evolved or changed.
/***c language Simulation The implementation of the go language to polymorphism * Define an interface, a class implements all functions of the interface, * This class is the implementation of the interface, does not explicitly declare a class implementation of the interface * * in the C language, define a structure containing a set of function pointers analog interface * A subclass has a function that creates the struct, which means that the subclass implements the interface */#include <stdlib.h> #include <stdio.h> #include <string.h> #ifndef safe_ Free#define Safe_free (p) \do \{\ Free (p); \ p = NULL; \} while (0) #endif//safe_free/*** defines an interface *cthis is a pointer to the class that implements the interface *e At is the function that the interface contains (function pointer) */typedef struct _i_animal{void *cthis; void (*eat) (void *cthis);} The i_animal;/*** data type rabbit* contains an attribute name*/typedef struct _rabbit{char *name;} Rabbit;/***rabbit implements I_animal interface required functions */void Rabbit_eat (void *cthis) {printf ("%s eat!\n", ((rabbit*) cthis)->name);} /*** means that rabbit implements the I_animal interface * The function returns rabbit implementation of the interface I_animal */i_animal *rabbit_i_animal (void *cthis) {I_animal *instance = (i _animal*) malloc (sizeof (i_animal)); if (null = = instance) {return null; } memset (instance, 0, sizeof (i_animal)); Instance->cthis = CThis; Instance->eat = Rabbit_eat; return instance;} /***rabbit constructor */rabbit *rabbit_create (void) { int namelength = 10; Rabbit *instance = (rabbit*) malloc (sizeof (rabbit)); if (null = = instance) {return null; } memset (instance, 0, sizeof (rabbit)); Instance->name = (char*) malloc (namelength*sizeof (char)); if (NULL! = instance->name) {memset (instance->name, 0, namelength*sizeof (char)); strcpy (Instance->name, "Rabbit"); } return instance;} /***rabbit's destructor */void rabbit_release (rabbit *cthis) {safe_free (cthis->name); Safe_free (cthis);} /*** data type cat* contains an attribute name*/typedef struct {char *name;} Cat;/***cat implements I_animal interface required function */void cat_eat (void *cthis) {print F ("%s eat!\n", ((cat*) cthis)->name);} /*** means that cat implements the I_animal interface * This function returns the cat's implementation of the interface I_animal */i_animal *cat_i_animal (void *cthis) {i_animal *instance = (i_animal*) malloc (sizeof (i_animal)); if (null = = instance) {return null; } memset (instance, 0, sizeof (i_animal)); Instance->cthis = CThis; Instance->eat = Cat_eat; return instance;} /***cat constructor */cat *cat_create (void) {int namelength = 10; Cat *instance = (cat*) malloc (sizeof (CAT)); if (null = = instance) {return null; } memset (instance, 0, sizeof (CAT)); Instance->name = (char*) malloc (namelength*sizeof (char)); if (NULL! = instance->name) {memset (instance->name, 0, namelength*sizeof (char)); strcpy (Instance->name, "cat"); } return instance;} /***cat destructor */void cat_release (cat *cthis) {safe_free (cthis->name); Safe_free (cthis);} /*** main function */int main () {//variable int i = 0 for loop traversal; The length of the array int animals_length = 2; Create Rabbit instance Rabbit *rabbit_instance = Rabbit_create (); Create Cat instance Cat *cat_instance = Cat_create (); Save an array of interface implementations I_animal *animals[2] = {NULL}; Create an implementation of rabbit to interface i_animal animals[0] = Rabbit_i_animal (rabbit_instance); Create a cat-to-interface i_animal implementation Animals[1] = Cat_i_animal (cat_instance); Analog polymorphism for (i = 0; i < animals_length; ++i) {//null pointer judgment if (NULL = = Animals[i]) {continue; }//The same piece of code, different bindings, the results will be different animals[i]->eat (animals[i]->cthis); The interface is created by a subclass corresponding function and requires manual release of Safe_free (Animals[i]); }//destructor rabbit rabbit_release (rabbit_instance); Destruction of Cat cat_release (cat_instance); return 0;}
Reference:
1, "I prefer the C language object-oriented programming paradigm" cloud wind BLOG
2, "Go language preliminary" cloud wind BLOG
3, "I use the C language object-oriented paradigm"
4, "Go for C + + programmers (c + + Programmer's Guide)"
5. OO in C (1): Class simulation and polymorphism in C language, inheritance
6, "C language Object-oriented implementation---polymorphism"