The Art of C++ Friendship - The C++ Friend Function Explained
summary
Concrete rules and examples for when friendship helps: friend grants full private/protected access but is not inherited, transitive, or mutual; use it for non-member customization points (streaming, arithmetic) to enable ADL and produce far shorter compile errors and faster name-lookup than sprawling overload sets. Alternatives shown: a constructor key to avoid broad friend access and an attorney–client idiom to expose only a make function. A hidden friend injection base-class pattern makes binary operators symmetric for templates and works in a units library.
AI-written summary · about
chapters (22)
- 0:00Introduction
- 1:14What is a friend in C++
- 3:44Streaming operator problem
- 8:17Binary operators and getters
- 11:11Why friend keyword exists
- 12:39Friend access and coupling
- 14:05Who can be a friend
- 16:17Friend member functions
- 22:10Does friendship break encapsulation
- 26:26Rules of friendship in C++
- 29:18When not to use friends
- 33:38Friendship as strong coupling
- 34:41Secure session example
- 38:55Template friend functions
- 44:17Compile-time performance issues
- 46:25Hidden friend functions
- 50:03Recommendations for customization points
- 50:43Hidden friend pitfalls
- 53:14Hidden friend injection idiom
- 56:28Summary of friend usage
- 57:31Alternatives to friendship
- 57:52Conclusion and Q&A
