Name hiding cpp

In case of virtual functions , redefining inherited methods is not a variation of overloading. If we redefine a function in a derived class which is virtual in base class, it doesn’t just override the base class declaration with the same function signature. It hides all base-class methods of the same name, regardless of the argument signature.

If we redefine just one version, the other one become hidden and cannot be used by objects of the derived class.

class ParentClass {
public:
virtual void someFunc(int a){
printf(” ParentClass :: someFunc (int) \n”);
};

virtual void someFunc(int* a){
printf(” ParentClass :: someFunc (int*) \n”);
};
};

class ChildClass : public ParentClass {
public:
virtual void someFunc(int* a){
printf(” ChildClass :: someFunc(int*) \n”);
};
};

int main(){
ChildClass obj;
/* This function call results in an error: */
obj.someFunc(2);
}

To avoid name hiding , use :

using ParentClass::someFunc();

Why name hiding in c++ is introduced ?

Consider following example:

class ParentClass
{
public:
void someFunction(string s){ };
};

class ChildClass : public ParentClass
{
public:
int anotherFunction(int i){};

/*This will use the same version of someFunction
that is in the ParentClass, since it does not have
it’s own definition of someFunction
*/
};

class GrandChildClass : public ChildClass
{
/* This overrides someFunction, creating
a new version */
public:
float someFunction(float i){};

};

Let’s suppose that there was no name hiding. Then this means that in the GrandChildClass, both versions of someFunction are now visible including the one in the parent class ParentClass – so float someFunction(float i), and void someFunction(string s) are both visible in the GrandChildClass class. If a call to “someFunction(2)” is made in either the ChildClass or the ParentClass, then it would obviously use the “void someFunction(string s){ };” version of the function since that is the only version of the function that those classes can “see”.

However, in the GrandChildClass if a call to “someFunction(29)” is made then there are 2 different functions that are visible – both someFunction(float i) and someFunction(string s) . It will obviously use someFunction(float i) since that is a better match for the parameter of 2. But, the issue here is that in the entire hierarchy of classes, the version of someFunction that is used is typically someFunction(string s), but once we get to the GrandChildClass, a different version of the someFunction function is used.

This type of behavior was then considered to be undesirable by the C++ standard writers, so they implemented name hiding, which effectively gives each class a “clean slate” or a new, clean class that does not carry over the scope of inherited functions with the same name.

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.