Linux驱动中继承与多态思想_C Hello

1. 定义全局模板函数集

//thermal_of.c
static struct thermal_zone_device_ops of_thermal_ops = {
    .get_trip_type = of_thermal_get_trip_type,
    .get_trip_temp = of_thermal_get_trip_temp,
    .set_trip_temp = of_thermal_set_trip_temp,
    .get_trip_hyst = of_thermal_get_trip_hyst,
    .set_trip_hyst = of_thermal_set_trip_hyst,
    .get_crit_temp = of_thermal_get_crit_temp,

    .bind = of_thermal_bind,
    .unbind = of_thermal_unbind,
};

3. 使用时继承全局模板函数集

//thermal_of.c
int __init of_parse_thermal_zones(void)
{
    ...
    struct thermal_zone_device_ops *ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
    ...
}

kmemdup 就是直接拷贝一份,拷贝的都是指针,这样就可以修改自己的,实现多态。

原文地址:https://www.cnblogs.com/hellokitty2/p/15724411.html