Skip to content

Instantly share code, notes, and snippets.

@UserXGnu
Last active October 30, 2017 01:03
Show Gist options
  • Save UserXGnu/accbfe66e2b78ccfcd808a3d77a53036 to your computer and use it in GitHub Desktop.
Save UserXGnu/accbfe66e2b78ccfcd808a3d77a53036 to your computer and use it in GitHub Desktop.
#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