11 ms·
what does an "object" look like? how do i derive from it?
by zabzonk 4y ago
what does an "object" look like? how do i derive from it?
- vidarh 4y agoAny struct. Only requirement would be that the first element can be casted to a point to a class object containing the vtable. I'ved edited to fix the casting so it actually compiles, and to use a void * in the argument list, as is what you'd actually usually do.
- Inviz 4y agogeneric base type struct
- rramadass 4y agoEasy; a subclass struct embeds the superclass struct as its first member. Simple example here: https://embeddedgurus.com/state-space/2008/01/object-based-programming-in-c/ https://embeddedgurus.com/state-space/2008/01/object-based-p... Lots more here: https://stackoverflow.com/questions/351733/how-would-one-write-object-oriented-code-in-c https://stackoverflow.com/questions/351733/how-would-one-wri...
- codr7 4y agoHaving spent quite some time down that rabbit hole back in the days, I'd definitely recommend against pushing C in that direction. Few people love GObject, for good reasons. Embedding combined with the container_of-macro [0] is a much more constructive approach from my experience. [0] https://stackoverflow.com/questions/15832301/understanding-container-of-macro-in-the-linux-kernel https://stackoverflow.com/questions/15832301/understanding-c...
- deleted 4y ago[deleted]