面向对象的 C 语言 发表于 2022-07-18 更新于 2022-07-21 分类于 C和CPP 模拟 C++ 使用的方法:虚函数表https://github.com/ityuhui/ooc/blob/main/main.c Linux内核采用的方法:container_of根据 成员的首地址 得到 结构体变量的地址。 应用场景:结构体(Child)通过包含结构体(Parent)的方式实现继承,当获得了结构体(Parent)的指针时,通过此函数可以得到结构体(Child) 泛型123456789101112131415struct _GValue{ GType g_type; union { gint v_int; guint v_uint; glong v_long; gulong v_ulong; gint64 v_int64; guint64 v_uint64; gfloat v_float; gdouble v_double; gpointer v_pointer; } data[2];};