Struct Inheritance in C

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

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

12条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 00:53

    You can do the above mentioned

    typedef struct
    {
        // base members
    
    } Base;
    
    typedef struct
    {
        Base base;
    
        // derived members
    
    } Derived;
    

    But if you want to avoid pointer casting, you can use pointers to a union of Base and Derived.

提交回复
热议问题