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 diagramElaborated type specifier syntax
   | +-struct-+  '-::-'  '-| nested_name_specifier |-'                        |
   | +-union--+                                                               |
   | '-enum---'                                                               |
               '-::-'                         '-+----------+--template_name-'
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;

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.

Related information