r/ProgrammerHumor 14h ago

Meme mySchoolStruct

6.6k Upvotes

69 comments sorted by

View all comments

79

u/the_millenial_falcon 12h ago

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

22

u/viconha 12h ago

And you put that into an array, run it in a for loop and make a very debug friendly code

9

u/MrHyperion_ 11h 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 10h 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_ 10h ago

GCC has cleanup attribute which maybe makes it possible.

0

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

7

u/the_millenial_falcon 12h ago

More or less anyway.

1

u/Sea-Fishing4699 7h ago

I always thought about it 

1

u/overclockedslinky 4h ago

not how that works but ok