Skip to content

Instantly share code, notes, and snippets.

@Irwin1985
Created January 4, 2022 21:58
Show Gist options
  • Select an option

  • Save Irwin1985/80dc23ccdc0f3aee310de3a6b70b87c0 to your computer and use it in GitHub Desktop.

Select an option

Save Irwin1985/80dc23ccdc0f3aee310de3a6b70b87c0 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
struct AbstractBaseClass {
char tipo;
};
struct Entero {
char tipo;
int dato;
};
struct Double {
char tipo;
double dato;
};
void prueba(void *p) {
struct Entero *p_e;
struct Double *p_d;
switch(((struct AbstractBaseClass*)p)->tipo) {
case 1: // Entero
p_e = (struct Entero*) p;
printf("El dato entero vale %d\n", p_e->dato);
break;
case 2: // Double
p_d = (struct Double*) p;
printf("El dato double vale %f\n", p_d->dato);
break;
default:
printf("Dato de tipo desconocido\n");
}
}
int main()
{
struct Entero *pe = malloc(sizeof(struct Entero));
struct Double *pd = malloc(sizeof(struct Double));
pe->tipo = 1;
pe->dato = 100;
pd->tipo = 2;
pd->dato = 3.1415;
prueba((void*) pe);
prueba((void*) pd);
pe->tipo = 3;
prueba((void*) pe);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment