Last active
October 30, 2017 01:03
-
-
Save UserXGnu/accbfe66e2b78ccfcd808a3d77a53036 to your computer and use it in GitHub Desktop.
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 _a { | |
void (*msg)(void); | |
} A; | |
typedef struct _b { | |
A parent; // herda as características de A | |
void (*msg)(void); | |
} B; | |
B* self; | |
void | |
msg_a(void) { | |
printf("Msg de A\n"); | |
} | |
void | |
msg_b(void) { | |
self->parent.msg(); | |
printf("Msg de B\n"); | |
} | |
void | |
init_a(A* a) { | |
a->msg = msg_a; | |
} | |
void | |
init_b(B* b) { | |
init_a(&(b->parent)); | |
b->msg = msg_b; | |
self = b; | |
} | |
int | |
main(int argc, char** argv) { | |
B b; | |
init_b(&b); | |
b.msg(); | |
// o exemplo da questão ficaria assim: | |
A* ptrA = &b; // Compilador vai dar Warning aqui, pra evitar faça A* ptrA = (A *)&b; | |
ptrA->msg(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment