Identifier Scope, Storage Class, and Linkage

In C++, we provide names for the entities we create, the variables, functions, and data types (such as class names) in our programs. These names, or identifiers, have various attributes.

A variable has

A function or method has


C++ variable and function names can be used only in certain regions of the program. This area is called the scope of the name. Scope determines the "lifetime" of automatic variables. Scope also determines the visibility of a name, when class constructors and destructors are called, and when variables local to the scope are initialized. There are many kinds of scope in C++; for our purposes, three of them are important:

Variable Shadowing

Variable shadowing occurs when a variable declared within a given scope has the same name as a variable declared in an outer scope. The outer variable is said to be shadowed. This can lead to confusion, as it may be unclear which variable subsequent uses of the variable name refer to. Here are some examples of shadowing:

In the following code fragments, the scope of the outer variable is shown highlighted in blue. The scope of inner variables that shadow the outer variable is shown highlighted in yellow or green.

A local variable in an inner block shadows a
local variable declared in an outer block
with the same name
Local variables shadow a global variable with the
same name
A local variable shadow a data member with the
same name
Inner x shadows outer x Local x shadows global x Local num shadows data member num

Accessing Shadowed Variables

In some cases, it is still possible to access a shadowed variable.

Storage Class

The storage class of a variable determines its "lifetime", how long it remains in the computer's memory. Variables in C++ essentially fall into one of three storage classes:


Linkage determines whether identifiers that have identical names refer to the same entity, even if those identifiers appear in different source files. The linkage of an identifier depends on how it was declared. There are three types of linkages: external, internal, and no linkage.