There are four types of scope. The names and properties of these four types of
scope may be defined as follows.
A name that is declared outside all blocks and
classes is said to have file or global scope.
Following its declaration, such a name may be used within the
translation unit of its occurrence.
- A name that is declared within a block
is said to have local scope. Subsequent to its declaration, such a name may be used only
within the block in which it is declared. The
formal parameters of a function
have local scope and are visible to the outermost block of the function.
- The names of the members of a
class have scope local to the class.
Methods of the class (or the methods of classes
deriving therefrom) may access such names without
qualification. Non-member functions are required to access
non-static members of a
class via the
member access operators . and ->.
The operator . is applied to an instance of a class. The operator
-> is applied to a pointer to an instance of a class. A class that is first
declared as a parameter or return type has global scope.
- Identifiers used to label statements are of
function scope. Only label identifiers have function scope. Label identifiers may be
used anywhere within the function in which they are defined.
A name becomes visible as soon as its declarator
is seen by the compiler, and before its initializer (if any)
is processed. Special rules apply to the names declared as
formal parameters of a
See the topic titled scope rules in the subsection
on inheritance for a summary of the rules that apply to the concept
Visibility of Names
The following applies to the visibility of names.
- A name that is redeclared within a block or
class is hidden.
- The hidden names of functions,
or objects that are of file scope may be accessed by prefixing them with ::
(the scope resolution operator).
- Hidden member names may be accessed by using the scope resolution operator ::
to qualify the member name with the class name (see the topics of
primary expressions and
static members and the subsection discussing
- Within a scope, if a class name matches the name of a function,
enumerator or object then the class name is hidden (order of declaration unimportant).
If a class name is hidden in this manner then it may still be used provided that it is
appropriately prefixed with either class, structure or union
(i.e. by using an elaborated type specifier).
- Similarly, a hidden enumerated type name may be accessed
by prefixing it with enumeration.