C++ Name Hiding — Complete Guide
이 글의 핵심
C++ name hiding C++, "function, visible", Introduction: "Base class function not visible" explained in detail with practical examples.
Introduction: “Base Class Function Not Visible"
"Overload Disappeared”
In C++, defining function with same name in derived class causes name hiding problem where all base class overloads get hidden.
Here is detailed implementation code using C++. Define a class to encapsulate data and functionality. Understand the role of each part while examining the code.
// ❌ Name hiding
class Base {
public:
void foo(int x) {
std::cout << "Base::foo(int): " << x << '\n';
}
void foo(double x) {
std::cout << "Base::foo(double): " << x << '\n';
}
};
class Derived : public Base {
public:
void foo(std::string s) {
std::cout << "Derived::foo(string): " << s << '\n';
}
};
int main() {
Derived d;
d.foo("Hello"); // OK
d.foo(42); // ❌ Compile error: no matching function
d.foo(3.14); // ❌ Compile error
}
What This Guide Covers:
- What is name hiding?
- Solution with using declaration
- Overloading vs overriding
- Practical examples
1. What is Name Hiding?
Name Hiding Occurs
Here is detailed implementation code using C++. Define a class to encapsulate data and functionality, ensure stability through error handling. Understand the role of each part while examining the code.
class Base {
public:
void foo(int x) {
std::cout << "Base::foo(int)\n";
}
};
class Derived : public Base {
public:
void foo(double x) { // Hides Base::foo(int)
std::cout << "Derived::foo(double)\n";
}
};
int main() {
Derived d;
d.foo(3.14); // Derived::foo(double)
d.foo(42); // ❌ Compile error
// error: no matching function for call to 'Derived::foo(int)'
}
Reason: Derived class foo completely hides base class foo.
2. using Declaration
Solution: using Declaration
Here is detailed implementation code using C++. Define a class to encapsulate data and functionality. Understand the role of each part while examining the code.
class Base {
public:
void foo(int x) {
std::cout << "Base::foo(int)\n";
}
void foo(double x) {
std::cout << "Base::foo(double)\n";
}
};
class Derived : public Base {
public:
using Base::foo; // ✅ Bring base class foo
void foo(std::string s) {
std::cout << "Derived::foo(string)\n";
}
};
int main() {
Derived d;
d.foo(42); // Base::foo(int)
d.foo(3.14); // Base::foo(double)
d.foo("Hello"); // Derived::foo(string)
}
3. Overloading vs Overriding
Overloading: Same Name, Different Signature
Below is an implementation example using C++. Define a class to encapsulate data and functionality. Try running the code directly to check its operation.
class MyClass {
public:
void foo(int x) {}
void foo(double x) {} // Overloading
void foo(std::string s) {} // Overloading
};
Overriding: Redefining Virtual Function
Below is an implementation example using C++. Define a class to encapsulate data and functionality. Understand the role of each part while examining the code.
class Base {
public:
virtual void foo(int x) {
std::cout << "Base::foo\n";
}
};
class Derived : public Base {
public:
void foo(int x) override { // Overriding
std::cout << "Derived::foo\n";
}
};
Name Hiding vs Overriding
Here is detailed implementation code using C++. Define a class to encapsulate data and functionality. Understand the role of each part while examining the code.
class Base {
public:
virtual void foo(int x) {
std::cout << "Base::foo(int)\n";
}
virtual void foo(double x) {
std::cout << "Base::foo(double)\n";
}
};
class Derived : public Base {
public:
void foo(int x) override { // Override Base::foo(int)
std::cout << "Derived::foo(int)\n";
}
// Base::foo(double) is hidden!
};
int main() {
Derived d;
d.foo(42); // Derived::foo(int)
d.foo(3.14); // ❌ Compile error
}
Solution:
Below is an implementation example using C++. Define a class to encapsulate data and functionality. Try running the code directly to check its operation.
class Derived : public Base {
public:
using Base::foo; // ✅ Bring Base::foo(double)
void foo(int x) override {
std::cout << "Derived::foo(int)\n";
}
};
Summary
Key Points
- Name hiding: Derived class function hides all base overloads
- using declaration: Brings base class functions
- Overloading: Same name, different signature
- Overriding: Redefining virtual function
- Solution: Use using Base::func;
When to Use
✅ Use using when:
- Want to keep base class overloads
- Derived class adds new overload
- Need all function variants
❌ Don’t use when:
- Want to completely replace base function
- Intentionally hiding base function
Best Practices
- ✅ Use using declaration for overloads
- ✅ Use override for virtual functions
- ✅ Be aware of name hiding
- ❌ Don’t accidentally hide base functions
- ❌ Don’t confuse overloading with overriding
Related Articles
Master name hiding for cleaner C++ inheritance! 🚀
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. Everything about C++ Name Hiding : from basic concepts to practical applications. Master key content quickly with exampl… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- C++ 함수 오버로딩 | ‘Function Overloading’ 가이드
- C++ 가상 소멸자 | ‘메모리 누수’ 상속 클래스 소멸자 에러 해결
- C++ 슬라이싱 문제 | ‘객체가 잘렸어요’ 상속 복사 에러 해결
이 글에서 다루는 키워드 (관련 검색어)
C++, name-hiding, inheritance, overloading, overriding, using, function 등으로 검색하시면 이 글이 도움이 됩니다.