--- id: cpp-virtual-functions title: "C++ Virtual Functions" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["virtual keyword", "override keyword", "pointer type vs object type", "C++ 가상 ν•¨μˆ˜"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "oop", "virtual-functions", "polymorphism"] raw_sources: ["https://www.w3schools.com/cpp/cpp_virtual_functions.asp"] applied_in: [] github_commit: "" --- # [[CPP Virtual Functions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Without the `virtual` keyword, a base-class pointer calls the function based on its DECLARED TYPE (`Animal*`), completely ignoring what it actually POINTS TO (a `Dog` object) β€” the source's own example proves this: `Animal* a = &d;` (where `d` is a `Dog`) still calls `Animal::sound()`, not `Dog::sound()`, UNTIL `virtual` is added to the base method, at which point the SAME pointer-based call correctly resolves to the actual object's type β€” meaning polymorphism through pointers doesn't work AT ALL by default in C++, and `virtual` is the specific opt-in required to make it happen. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Virtual function** β€” a base-class member function that CAN be overridden in derived classes and resolved based on the ACTUAL object, not the pointer's declared type. [S1] - **Without `virtual`** β€” a base-class pointer always calls the BASE class's version of a method, even if it points to a derived-class object. [S1] - **With `virtual`** β€” the SAME pointer call now resolves to the DERIVED class's overridden version β€” this is how runtime polymorphism actually works in C++. [S1] - **`override` keyword** β€” optional but recommended in the derived class, for clarity (documents that this method is intentionally overriding a virtual base method). [S1] - **`->` operator** β€” accesses members through a pointer; shorthand for `(*pointer).member`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Without virtual β€” the base class's version runs despite the pointer pointing to a Dog: `class Animal { public: void sound() { cout << "Animal sound\n"; } }; class Dog : public Animal { public: void sound() { cout << "Dog barks\n"; } }; Animal* a; Dog d; a = &d; a->sound(); // "Animal sound" β€” WRONG, ignores actual object type`. [S1] - With virtual β€” the derived class's version correctly runs: `class Animal { public: virtual void sound() { cout << "Animal sound\n"; } }; class Dog : public Animal { public: void sound() override { cout << "Dog barks\n"; } }; Animal* a; Dog d; a = &d; a->sound(); // "Dog barks" β€” CORRECT`. [S1] - The `->` shorthand explained: `Animal* a = new Animal(); a->sound(); // same as (*a).sound();`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **virtual μ—†μ΄λŠ” 포인터 기반 λ‹€ν˜•μ„±μ΄ μ „ν˜€ μž‘λ™ν•˜μ§€ μ•ŠμŒ**: Animal 포인터가 μ‹€μ œλ‘œλŠ” Dog 객체λ₯Ό 가리킀고 μžˆμ–΄λ„ virtual이 μ—†μœΌλ©΄ 항상 Animal::sound()κ°€ ν˜ΈμΆœλœλ‹€λŠ” 점이 직접 예제둜 증λͺ…됨 β€” virtual을 λΆ™μ—¬μ•Όλ§Œ μ‹€μ œ 객체 νƒ€μž… 기반으둜 μ˜¬λ°”λ₯΄κ²Œ 해석됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” 베이슀 클래슀 ν¬μΈν„°λ‘œ νŒŒμƒ 클래슀 객체λ₯Ό 가리킀고 μ˜¬λ°”λ₯Έ μ˜€λ²„λΌμ΄λ“œ λ©”μ„œλ“œκ°€ ν˜ΈμΆœλ˜λ„λ‘ ν•˜λŠ” 것이 μ‹€μ „ λ‹€ν˜•μ„± κ΅¬ν˜„μ˜ 핡심 μš”κ΅¬μ‚¬ν•­μ΄λ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) virtual making a base-class pointer correctly resolve to the derived class's overridden method (C++): ```cpp class Animal { public: virtual void sound() { cout << "Animal sound\n"; } }; class Dog : public Animal { public: void sound() override { cout << "Dog barks\n"; } }; int main() { Animal* a; Dog d; a = &d; a->sound(); // Outputs: Dog barks (correct, thanks to virtual) return 0; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Polymorphism]], [[CPP Pointers]], [[CPP Friend Function]] - **μ°Έμ‘° λ§₯락:** 가상 ν•¨μˆ˜ β€” friend ν•¨μˆ˜(Friend Function) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Virtual Functions β€” https://www.w3schools.com/cpp/cpp_virtual_functions.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Virtual Functions" page (Astra wiki-curation, P-Reinforce v3.1 format).