Will this trick work in C?

后端 未结 5 973
终归单人心
终归单人心 2021-01-21 15:50

I want to add a field to a structure in C. So for example I have the following structure.

struct A
{
 some_type x;
 some_type y;
}

I declare a

5条回答
  •  旧巷少年郎
    2021-01-21 16:55

    Yes, it would work. A a will be the first member in the struct

    This is how some people simulated OO inheritance in C

    You may use

      &b->a
    

    instead of the cast. And probably do an ASSERT like

     ASSERT (&b->a == b)
    

    to be warned when you accidentally destroyed this semantic

提交回复
热议问题