Struct Inheritance in C

后端 未结 12 1650
悲&欢浪女
悲&欢浪女 2020-11-29 00:04

Can I inherit a structure in C? If yes, how?

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 00:50

    If your compiler supports anonymous structs, you can do this:

    typedef struct Base
    {
        // base members
    } Base_t;
    
    typedef struct
    {
       struct Base;  //anonymous struct
    
       // derived members
    
    } Derived_t;
    

    This way, base stuct members can be acessed directly, which is nicer.

提交回复
热议问题