3 ms·
This is more or less what GObject is, which the author mentions in passing. It's an OO system for C, but it does require quite a lot of boilerplate, and you ne
by kelnos 4y ago
This is more or less what GObject is, which the author mentions in passing. It's an OO system for C, but it does require quite a lot of boilerplate, and you need to manually initialize vtables when creating new subclasses. You also need to manually chain up to the superclass in virtual methods, in many places where it's easy to forget. It's a decent system, all things considered, but it's just a reminder that C's type system is very weak and implementing advanced features all but requires abuse of the preprocessor to avoid unreadable code.
- Dwedit 4y agoWhile you do need to manually initialize a vtable pointer, that pointer can simply point to a const struct which lives in the read-only data section. You don't need to allocate a new vtable with each object or anything like that. And you can do COM objects in C without using the preprocessor at all (outside of the "are we C++ or not" condition, then you could use real classes instead)
- rzzzt 4y agoCan you emulate partial function application somehow in C or does the caller still need to supply "this" as an argument to instance methods?
- ablob 4y agoYou can emulate partial function application. You also always need to supply some kind of "this" if you don't want your data to live in static memory.
- wruza 4y agoGObject doesn’t abuse the preprocessor. It suggests to #define type-guard parts of the standard boilerplate (purely in spirit of C), but doesn’t require you to do so. Everything else is just C. Also, being an object system, not only a type system, it is more advanced than C++ in parts of properties and signals. Of course that requires some cooking. For example, Qt had to resort to a custom pre-compiler for that. That said, I believe it loses quickly in ergonomics to plain Objective C, which is best “practical C + objects” flavor ever done, imo.
- pjmlp 4y agoDepending on how much one likes to type @.