--- id: cpp-friend-function title: "C++ The Friend Keyword" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["friend function", "bypass encapsulation", "C++ friend ν‚€μ›Œλ“œ"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "oop", "friend", "encapsulation"] raw_sources: ["https://www.w3schools.com/cpp/cpp_friend_function.asp"] applied_in: [] github_commit: "" --- # [[CPP The Friend Keyword]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A `friend` function is deliberately EXEMPT from the entire Encapsulation chapter's rule β€” it's NOT a member of the class at all, yet it can read `private` data directly with no getter method required, meaning `friend` is a language-level ESCAPE HATCH from encapsulation, granted explicitly and individually per-function rather than a general loophole, since each `friend` declaration must name exactly which external function gets the privilege. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Friend function** β€” a function that is NOT a member of the class, but is explicitly GRANTED access to the class's `private` members. [S1] - **Declared inside, defined outside** β€” the friend function's declaration lives INSIDE the class (marked with `friend`), but its actual body is written OUTSIDE the class like a normal function. [S1] - **Bypasses the getter/setter requirement** β€” unlike ordinary external code (which must use public getters/setters per the Encapsulation chapter), a friend function reads `private` members directly. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - A friend function accessing a private attribute directly: `class Employee { private: int salary; public: Employee(int s) { salary = s; } friend void displaySalary(Employee emp); }; void displaySalary(Employee emp) { cout << "Salary: " << emp.salary; } Employee myEmp(50000); displaySalary(myEmp);` β€” `displaySalary` isn't a class member but reads `emp.salary` directly. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **friendλŠ” μΊ‘μŠν™” κ·œμΉ™μ˜ μ˜ˆμ™Έ**: 일반적으둜 private λ©€λ²„λŠ” public κ²Œν„°/μ„Έν„°λ‘œλ§Œ μ ‘κ·Όν•΄μ•Ό ν•˜μ§€λ§Œ, friend둜 μ„ μ–Έλœ ν•¨μˆ˜λŠ” 클래슀의 멀버가 μ•„λ‹˜μ—λ„ private 데이터λ₯Ό 직접 읽을 수 μžˆλ‹€λŠ” μ μ—μ„œ μΊ‘μŠν™” μ›μΉ™μ˜ λͺ…μ‹œμ  μ˜ˆμ™Έμž„μ΄ 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) Employee의 private κΈ‰μ—¬(salary)λ₯Ό friend ν•¨μˆ˜ displaySalary()κ°€ κ²Œν„° 없이 직접 좜λ ₯ν•˜λŠ” μ˜ˆμ œκ°€ friend ν‚€μ›Œλ“œμ˜ λŒ€ν‘œ μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ 원문에 직접 μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) A friend function reading a private member directly, with no getter needed (C++): ```cpp class Employee { private: int salary; public: Employee(int s) { salary = s; } friend void displaySalary(Employee emp); // friend declaration }; void displaySalary(Employee emp) { // defined outside, not a member cout << "Salary: " << emp.salary; // direct access to private data } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Virtual Functions]], [[CPP Encapsulation]], [[CPP Templates]] - **μ°Έμ‘° λ§₯락:** friend ν•¨μˆ˜ β€” ν…œν”Œλ¦Ώ(Templates) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ The Friend Keyword β€” https://www.w3schools.com/cpp/cpp_friend_function.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ The Friend Keyword" page (Astra wiki-curation, P-Reinforce v3.1 format).