[2026] C++ friend Keyword: Access Control, Operators, and Encapsulation

[2026] C++ friend Keyword: Access Control, Operators, and Encapsulation

이 글의 핵심

friend functions and classes grant access to private and protected members. When to use friend for operators and factories, and how to avoid excessive 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. 아래 코드는 cpp를 사용한 구현 예제입니다. 클래스를 정의하여 데이터와 기능을 캡슐화하며. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

// 타입 정의
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<<, symmetric operator+)
  • Helper functions tightly coupled to a class
  • Collaborating classes that must see internals
  • Factories calling private constructors friend categories:
  1. friend functions
  2. friend classes
  3. 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 functionfriend class
ScopeSpecific free functions (or overload set)All methods of the friend class
CouplingUsually smallerOften 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.

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?

C++, friend, access control, OOP, operators

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3