Hamutaro - Hamtaro 4

Backend/C++

[C++] ์ด๋ฆ„ ์€ํ (name hiding)

carsumin 2024. 12. 4. 22:44
์ด๋ฆ„ ์€ํ (name hiding)
  • ์˜ˆ์ œ 1
void f(int x){
    cout << "f(int x) --> " << x << endl;
}
void f(double x){
    cout << "f(double x) --> " << x << endl;
}
int main(){
    f(10);
    f(20.0);
}

 

  • ์ถœ๋ ฅ
f(int x) --> 10
f(double x) --> 20

 

 

  • ์˜ˆ์ œ 2
void f(int x){
    cout << "f(int x) --> " << x << endl;
}
void f(const char* x){
    cout << "f(const char* x) --> " << x << endl;
}
int main(){
    void f(int x);
    f(10); //void f(int x);
    f("abc"); //์—๋Ÿฌ
    ::f("abc"); //void f(const char* x)
}

 

  • ์˜ˆ์ œ 3
class A {
public:
    void f(int x) { cout << "A::f() --> " << x << endl; }
};
class B : public A {
public:
    void f(double x) { cout << "B::f() --> " << x << endl; }
};
int main(){
    B objB;
    objB.f(10.0);
    objB.f(20);
}

 

  • ์ถœ๋ ฅ
B::f() --> 10
B::f() --> 20