Programming Language

Explanation Of Name Hiding In C++

If the enumeration name or class name is not hidden and is in scope, this means it is visible. The enumeration name or class name can be hidden through an explicit declaration of that same name — as an enumerator, object or function— in a nested derived class or declarative region. The enumeration name or class name is hidden wherever the enumerator, object or function name is visible. This whole procedure is known as name hiding.

If you want to understand Name hiding, then look at the below example, as name hiding in C++ is best explained by an example.


class AdultClass {


virtual void someFunc(int a) {

printf(“AdultClass :: someFunc (int) \n”);


virtual void someFunc (int*a) {

printf(“AdultClass :: someFunc (int*) \n”



class TeenagerClass : public AdultClass {


virtual void someFunc(int*a) {

printf(“TeenagerClass :: someFunc (int*) \n”);



int main () {

TeenagerClass obj;

/* This function call results in an error: */





Although, it seems like the call to the someFunc function in the major function in the example should run just fine. However, the reality is different because this procedure call, results in an error stating:

  • error: no match function for call to `TeenagerClass::someFunc(int)

The question arises that what is the reason behind that error? The TeenagerClass clearly derives from the AdultClass, therefore it seems like the TeenagerClass object in the major function should have admittance to the someFunc(int) function acknowledge inside the AdultClass in addition to the someFunc(int*) that recognized a numeral pointer and is defined inside the TeenagerClass. Thus, the reason behind the function call above that leads to an error is ultimately because of name hiding (a property of C++).

All the functions in derived classes having the same name as the adult class functions; however that do not supersede the adult classes’ function are believed to be bad practice as it can lead to errors just like the one stated above. Therefore, having a function such as someFunction is regarded as bad practice. This is because it results in ambiguity, and it’s much simpler to just give the functions unique names instead.

Understanding Name Hiding Error Message

Do you know why the above code resulted in an error message? This is because the name lookup that is operated by C++ will discontinue looking for other names when it discovers a single name in one of your base classes.  The reason behind this is the someFunction assertion in TeenagerClass basically hides the someFunction assertion in AdultClass.

Leave a Reply