C++ friend Keyword: Access Control, Operators, and Encapsulation
이 글의 핵심
friend lets designated functions or classes access private and protected members—useful for symmetric operators and tight collaborators; misuse raises coupling.
What is friend?
The friend keyword lets another class or function access private or protected members. It is a controlled escape hatch while keeping most details non-public.
class Box {
private:
int width;
public:
Box(int w) : width(w) {}
friend void printWidth(const Box& box);
};
void printWidth(const Box& box) {
std::cout << "Width: " << box.width << std::endl;
}
Why it exists:
- Operator overloading (
operator<<, symmetricoperator+) - Helper functions tightly coupled to a class
- Collaborating classes that must see internals
- Factories calling private constructors
friend categories:
- friend functions
- friend classes
- friend member functions (specific method of another class)
friend functions
Typical for distance, print, etc.—free functions declared inside the class with friend.
friend classes
All member functions of the friend class may access private members of the grantor—use carefully.
Operator overloading
operator<</>> are usually non-members with friend access to private fields. Symmetric binary operators are often non-member friends.
Practical examples
The Korean article includes Matrix multiply/operator<<, Fraction arithmetic, and stream operators for Person—code unchanged; principles above apply.
friend vs getters
friend narrows who can see internals; public getters expose to everyone.
Common pitfalls
Over-friending
Too many friend declarations increase coupling and review burden.
Bidirectional friends
Sometimes a sign of tangled design—consider interfaces or dependency inversion.
Friendship is not inherited
A function that is a friend of Base is not automatically a friend of Derived for Derived-only members.
Real patterns
- Factory functions that call private constructors (friend free function).
- Comparison operators as symmetric non-member friends.
- Narrow test helpers—team policy matters.
friend vs friend class (summary)
| friend function | friend class | |
|---|---|---|
| Scope | Specific free functions (or overload set) | All methods of the friend class |
| Coupling | Usually smaller | Often larger |
Operators (advanced)
- Symmetric binary ops as non-members.
operator+=often a member;operator+often built from+=or as non-member friend.
Encapsulation
friend is not “removing private”—it is opening the door to named collaborators. Prefer non-member non-friend APIs when feasible (Effective C++ Item 23 spirit).
FAQ
Covers when to use friend, encapsulation trade-offs, that friend functions are not members, friendship does not inherit, and comparison to getters.
Resources: Effective C++, C++ Primer, cppreference friend declaration.
Related posts (internal links)
- Inheritance and polymorphism
- static members
- Design patterns
Practical tips
Debugging
- Warnings first; small repro.
Performance
- Profile; friend has no runtime cost.
Code review
- Question every new friend.
Practical checklist
Before coding
- Minimal access?
- Maintainable?
- Clear ownership?
While coding
- Warnings?
- Invariants preserved?
During review
- Could this be non-friend?
Keywords (search)
C++, friend, access control, OOP, operators
Related posts
- C++ series
- Adapter pattern