Anonymous functions return dynamically allocated values

前端 未结 4 1963
北恋
北恋 2021-01-29 06:12

The question is based on a design pattern solution easily doable in other languages but difficult to implement in C. The narrowed down code is below.

Building on this an

4条回答
  •  旧时难觅i
    2021-01-29 06:13

    (Quoting your accepted answer to yourself)

    Secondly a pointer to a parent struct can't receive a pointer to it's derived type (Embedded parent struct) so I can't do much there. I tried using void * but perhaps a solution might exists using memory address and then access some member of the struct without casting to specific types. I'll ask that in another question.

    This is yet another pointer that one should learn the basics first. The thing you miss is called 'forward declaration':

    struct chicken; // here we tell the compiler that 'struct chicken' is a thing
    struct egg{
      struct chicken *laidby; // while the compiler knows no details about 'struct chicken',
                              // its existence is enough to have pointers for it
    };
    struct chicken{           // and later it has to be declared properly
      struct egg *myeggs;
    };
    

    What I'm missing is the ability to call the super method from the overridden run method in some way?

    These are not methods and there is no override. In your code no OOP happens, C is a procedural programming language. While there are OOP extensions for C, you really should not go for them without knowing C basics.

提交回复
热议问题