Created
June 11, 2012 04:19
-
-
Save emersonmx/2908495 to your computer and use it in GitHub Desktop.
um exemplo de polimorfismo em C!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef struct polymorph polymorph; | |
struct polymorph | |
{ | |
void* data; | |
int (*compare) (polymorph*, void*); | |
}; | |
int polymorph_compare(polymorph* p1, polymorph* p2) | |
{ | |
return p1->compare(p1, p2->data); | |
} | |
int compare_default(polymorph* p, void* data) | |
{ | |
if (p->data < data) | |
{ | |
return -1; | |
} | |
else if (p->data > data) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
int main(int argc, char* argv[]) | |
{ | |
if (argc != 3) | |
{ | |
printf("Usage: %s value1 value2\n", argv[0]); | |
return 0; | |
} | |
polymorph p1 = { (void*) atoi(argv[1]), &compare_default }; | |
polymorph p2 = { (void*) atoi(argv[2]), &compare_default }; | |
printf("p1 = %d\np2 = %d\n", (int) p1.data, (int) p2.data); | |
int r = polymorph_compare(&p1, &p2); | |
printf("p1 %s p2\n", ((r < 0) ? "<" : ((r > 0) ? ">" : "=="))); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment