#define myoffsetof(type, f) ((size_t) \
((char *)&((type *)0)->f - (char *)(type *)0))
typedef int (*print_ptr)();
struct animal
{
print_ptr print_cat_;
print_ptr print_dog_;
};
int main()
{
struct animal _animal = {
.print_cat_ = print_cat,
.print_dog_ = print_dog
};
size_t cat = myoffsetof(struct animal, print_cat_);
size_t dog = myoffsetof(struct animal, print_dog_);
bool is_dog = 0;
size_t run_define = is_dog ? dog : cat;
print_ptr print = *(print_ptr *)((char *)&_animal + run_define);
int ret = print();
return 0;
}
C运行时多态
最新推荐文章于 2024-09-14 18:59:16 发布