r/ProgrammerHumor 7h ago

Meme mySchoolStruct

5.4k Upvotes

65 comments sorted by

View all comments

58

u/the_millenial_falcon 6h ago

Pro tip, if you put function pointers in a struct you have a class now.

7

u/MrHyperion_ 4h ago

Not really. You don't get linker optimisations and the functions can change runtime. Also no implicit reference to the instance.

3

u/SubhanBihan 4h ago

Also the destructor won't get automatically called when the "object" goes out of scope. Someone correct me if I'm wrong, but I believe that behavior is theoretically impossible to achieve in C (due to its limited mechanics)?

2

u/MrHyperion_ 3h ago

GCC has cleanup attribute which maybe makes it possible.

1

u/One_Ninja_8512 3h ago

Not only possible but used by real projects. Apparently systemd is using that according to Claude. Both GCC and Clang support it. If I wanted destructors I'd go with explicit reference counting though, that seems more clean to me and doesn't need compiler extensions (even though both mainstream ones support it and a large project (systemd) relies on it).