トリッキーなコード

このエントリーをはてなブックマークに追加
414デフォルトの名無しさん
Cで
typedef void (*VirtualFunc)(void*);
typedef struct {
VirtualFunc hello;
} Animal;
typedef struct {
VirtualFunc hello;
Animal* super;
} Cat;

void helloAnimal(void* self) { printf("...\n"); }
void helloCat(void* self) { printf("にゃお〜\n"); }

Animal* newAnimal() {
Animal* animal = (Animal*)malloc(sizeof(Animal));
animal->hello = helloAnimal;
return animal;
}
Cat* newCat() {
Cat* cat = (Cat*)malloc(sizeof(Cat));
cat->super = newAnimal();
cat->super->hello = cat->hello = helloCat;
return cat;
}

int main() {
Animal* pet = (Animal*)newCat();
pet->hello(pet);
return 0;
}
とかやってた人いない?