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:

Read syntax diagramSkip visual syntax diagram
Elaborated type specifier syntax

>>-+-+-class--+--+----+--+---------------------------+--identifier------------+-><
   | +-struct-+  '-::-'  '-| nested_name_specifier |-'                        |   
   | +-union--+                                                               |   
   | '-enum---'                                                               |   
   '-typename--+----+--nested_name_specifier--+-identifier------------------+-'   
               '-::-'                         '-+----------+--template_name-'     
                                                '-template-'                      

Nested name specifier

|--+-class_name-----+--::--------------------------------------->
   '-namespace_name-'       

>--+---------------------------------+--------------------------|
   +-template--nested_name_specifier-+   
   '-nested_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.