←back to thread

218 points signa11 | 1 comments | | HN request time: 0.206s | source
Show context
throwaway7894 ◴[] No.43681266[source]
As someone who has a file with similar hacks, I will say this: I am not a C++ fan, but if you find yourself writing C code where you simulate methods via structs with function pointers often, just use C++ as a basic "C with classes" at that point. You want methods anyway, you have to go through a pointer dereference to call the function, it's just not worth the code weirdness. If you have the grit to use structs with function pointers everywhere, you have the grit to stick to the simpler subset of C++.
replies(5): >>43683169 #>>43683849 #>>43684044 #>>43701516 #>>43703558 #
uecker ◴[] No.43683169[source]
Why? I do not find the syntactic sugar C++ adds very helpful and it misses other C features.
replies(1): >>43704746 #
1. ryao ◴[] No.43704746[source]
Perhaps he wants to jump through hoops to avoid function pointers, even when doing that in C++ for optional functions like is done in the VFS requires gymnastics:

https://godbolt.org/z/4GWdvsz6z

That is the closest I can get it to implementing an optional function via a C++ class member function instead of a function pointer. It is not only insane, but also masochistic in comparison to how it would be done via function pointers:

https://godbolt.org/z/qG3v5zcYc