What is name hiding in C++?
Solution
In C++, when you have a class with an overloaded method,
and you then extend and override that method, you must override all of the overloaded methods; otherwise compiling error occurs.
#include <iostream>
using namespace std;
class A
{
public:
A(){;}
~A(){;}
virtual int F(int a, int b)
{
cout << "F(int a, int b) in A" << endl;
return 1;
}
virtual int F(bool b)
{
cout << "F(bool b) in A" << endl;
return 1;
}
};
class B:A
{
public:
B(){;}
~B(){;}
int F(int a, int b)
{
cout << "F(int a, int b) in B" << endl;
return 1;
}
};
int main(int argc, char* argv[])
{
A a;
B b;
a.F(1, 2);
a.F(false);
b.F(1,2);
//b.F(false); // Compile error: error C2660: 'B::F' : function does not take 1 arguments ---> Name hiding
return 0;
}