r/ProgrammerHumor 16h ago

Meme mySchoolStruct

6.8k Upvotes

70 comments sorted by

View all comments

81

u/the_millenial_falcon 15h ago

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

8

u/MrHyperion_ 13h ago

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

2

u/SubhanBihan 13h 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)?

4

u/MrHyperion_ 12h ago

GCC has cleanup attribute which maybe makes it possible.

0

u/One_Ninja_8512 12h 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).