3 ms·
Inheritance can be implemented as well: typedef struct AdvancedFoo { BaseFoo super; int a, b, c; thinkfunc_t thinkHarder; } AdvancedF
by nemetroid 13y ago
Inheritance can be implemented as well:
typedef struct AdvancedFoo {
BaseFoo super;
int a, b, c;
thinkfunc_t thinkHarder;
} AdvancedFoo;
Since `super` is the first member in the struct, a pointer to an AdvancedFoo can be used as a pointer to BaseFoo as well:
AdvancedFoo *advFoo = AdvancedFoo_new();
advFoo->thinkHarder(advFoo, advFoo->a);
((BaseFoo*) advFoo)->doThink(advFoo, some_int_method());
Of course, this inheritance pattern can continue in multiple steps.