I had an idea about how to implement interfaces in C. The idea is really simple, but when searching for questions discussing this, I did not see it mentioned.
So, the "standard" inheritance emulation method in C is essentially
struct dad {
arm_t right_arm;
arm_t left_arm;
}
struct kid {
struct dad parent;
diaper_t diaper;
}
Which I think is ugly, because kid.parent.right_arm
just makes less sense than kid.right_arm
. There is another way apparently, if you don't mind using gcc-exclusive flags, which is something like
struct kid {
struct dad;
diaper_t diaper;
}
but this is not portable. What is portable, however, is
// file: dad.interface
arm_t right_arm;
arm_t left_arm;
// file: kid.h
struct kid {
#include dad.interface
diaper_t diaper;
}
For the purposes of this question, I'm not interested in suggestions for alternatives. My question is simply this: what, if anything, is wrong with this approach?