Name hiding (C++ only)

If a class name or enumeration name is in scope and not hidden, it is visible. A class name or enumeration name can be hidden by an explicit declaration of that same name — as an object, function, or enumerator — in a nested declarative region or derived class. The class name or enumeration name is hidden wherever the object, function, or enumerator name is visible. This process is referred to as name hiding.

In a member function definition, the declaration of a local name hides the declaration of a member of the class with the same name. The declaration of a member in a derived class hides the declaration of a member of a base class of the same name.

Suppose a name x is a member of namespace A, and suppose that the members of namespace A are visible in a namespace B because of a using declaration. A declaration of an object named x in namespace B will hide A::x. The following example demonstrates this:

#include <iostream>
#include <typeinfo>
using namespace std;

namespace A {
  char x;
};

namespace B {
  using namespace A;
  int x;
};

int main() {
  cout << typeid(B::x).name() << endl;
}

The following is the output of the above example:

int

The declaration of the integer x in namespace B hides the character x introduced by the using declaration.

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]