Scope of class names (C++ only)

A class declaration introduces the class name into the scope where it is declared. Any class, object, function or other declaration of that name in an enclosing scope is hidden.

If a class name is declared in the same scope as a function, enumerator, or object with the same name, you must refer to that class using an elaborated type specifier:

Elaborated type specifier syntax

Read syntax diagramSkip visual syntax diagramclassstructunionenum::nested_name_specifieridentifiertypename::nested_name_specifieridentifiertemplatetemplate_name
Nested name specifier
Read syntax diagramSkip visual syntax diagram class_namenamespace_name ::templatenested_name_specifiernested_name_specifier
The following example must use an elaborated type specifier to refer to class A because this class is hidden by the definition of the function A():
class A { };

void A (class A*) { };

int main()
{
      class A* x;
      A(x);
}
The declaration class A* x is an elaborated type specifier. Declaring a class with the same name of another function, enumerator, or object as demonstrated above is not recommended.

An elaborated type specifier can also be used in the incomplete declaration of a class type to reserve the name for a class type within the current scope.