Private-members Memes

Posts tagged with Private-members

When You Are A Public Function But Only Friends Are Allowed To Touch Privates

When You Are A Public Function But Only Friends Are Allowed To Touch Privates
Object-oriented programming's access modifiers explained through the most awkward love triangle in computer science. You're out here being a public function, visible to the entire codebase, while watching your friend classes get all cozy with those private members you can only dream about accessing. It's like being the third wheel at a party where everyone's talking about encapsulation. Sure, you're public and everyone can call you, but that doesn't mean you get special privileges. Meanwhile, friend functions are living their best life, breaking all the rules of data hiding because they got that special declaration. The real kicker? Friend functions in C++ can access private and protected members even though they're not part of the class. They're basically the VIP pass holders of the programming world while you're stuck outside the velvet rope with the rest of the public interface.

Friends Will Be Friends

Friends Will Be Friends
Someone's asking if using friend classes is frowned upon, and the top comment drops the golden rule: "Don't let friends touch your privates." For context, the friend keyword in C++ lets another class access your private members, which is basically punching a hole through encapsulation. It's like giving someone the keys to your house and saying "please don't go through my underwear drawer." Most devs consider it a code smell because it creates tight coupling and defeats the purpose of access modifiers. If you need a friend class, your design probably needs a refactor. The double entendre here is *chef's kiss* — both a programming best practice AND life advice wrapped in one sentence.