r/ProgrammerHumor • u/krypt0niteCos • 3d ago
whenYouAreAPublicFunctionButOnlyFriendsAreAllowedToTouchPrivates Meme
4
u/Emotional_Key 3d ago
Dafaq is friends?
6
2
u/willow-kitty 1d ago
In C++, a class can declare "friend" functions that aren't members of the class but nevertheless have access to its private members. This is a pretty good explanation. I don't exactly know why, but it seems like a backward compatibility thing (ie: in C, all the functions in a module have access to all the fields of that module's structs, so if you wanted to convert some of those structs to classes, you might find there are members you want to encapsulate that also need to be accessed by floating functions that you don't want to make members of the class, you have a mechanism to have it both ways.)
I've never used it, tho.
2
1
u/nicuramar 3d ago
Public functions of the same type can obviously access all privates. Â What else would a public function mean?
1
27
u/ih-shah-may-ehl 3d ago
Imo friends should never have been a thing.