Extending a struct in C

前端 未结 11 912
你的背包
你的背包 2020-12-23 15:54

I recently came across a colleague\'s code that looked like this:

typedef struct A {
  int x;
}A;

typedef struct B {
  A a;
  int d;
}B;

void fn(){
  B *b;         


        
11条回答
  •  误落风尘
    2020-12-23 16:38

    This is perfectly legal, and, in my opinion, pretty elegant. For an example of this in production code, see the GObject docs:

    Thanks to these simple conditions, it is possible to detect the type of every object instance by doing:

    B *b;
    b->parent.parent.g_class->g_type
    

    or, more quickly:

    B *b;
    ((GTypeInstance*)b)->g_class->g_type
    

    Personally, I think that unions are ugly and tend to lead towards huge switch statements, which is a big part of what you've worked to avoid by writing OO code. I write a significant amount of code myself in this style --- typically, the first member of the struct contains function pointers that can be made to work like a vtable for the type in question.

提交回复
热议问题