Linux kernel: why do 'subclass' structs put base class info at end?

后端 未结 4 1980
走了就别回头了
走了就别回头了 2021-02-04 02:37

I was reading the chapter in Beautiful Code on the Linux kernel and the author discusses how Linux kernel implements inheritance in the C language (amongst other topics). In a

4条回答
  •  时光说笑
    2021-02-04 03:10

    It's for multiple inheritance. struct dev isn't the only interface you can apply to a struct in the linux kernel, and if you have more than one, just casting the sub class to a base class wouldn't work. For example:

    struct device {
         int a;
         int b;
         // etc...
    };
    
    struct asdf {
       int asdf_a;
    };
    
    struct usb_device {
        int usb_a;
        int usb_b;
        struct device dev;
        struct asdf asdf;
    };
    

提交回复
热议问题