Any identifier used in a C++ program (such as the name of a variable
or object, the name of a type or class, or the name of a named
constant) has a scope, i.e., a region of the program in which
that name can be used. Often we are mostly concerned with the scope of
variables and objects, since those are the things that we are generally
dealing with most of the time. Not only that, but if such an entity has
the wrong scope, or too wide a scope, things can easily come off the
rails.
In addition to its scope, a variable has many other properties of
interest. For example, just like humans, a variable (or an object)
lives in both space and time: the space it occupies in the computer's
memory, and the time during which it has that memory allocated to it
when the program is running.
Attributes of a Variable or Object
So, a variable (or object) has
- a name
- a type (which also determines a size, the amount of memory needed
to store a value of this type)
- a value (or not, if it has not been initialized or assigned
to)
- an address in memory (unless it is stored in a register)
- a scope (the region of the program where it can be seen or
used)
- a storage class (which determines its lifetime, that part of the
program execution time during which memory is allotted to it)
- a linkage (which is related to scope and storage class, and which
refers to whether or not, in a multi-file program, the variable is
known only in the current source file or in any source file having
the proper declarations)
Note that any program identifier has a scope,
while a variable or data-object identifier also has a
lifetime. Try not to confuse these two (quite independent)
notions.
Declarations and Definitions, Declarative Regions, Potential Scope
and Actual Scope
A variable (or object) also
- may be "declared" many times, but can be "defined" only once,
because a declaration simply says "there is such a thing", but a
definition actually reserves space in memory for that thing
- is declared in some "region" of the program (called its
"declarative region")
- has a "potential scope" which extends from the point at which its
definition or declaration is physically placed in its declarative
region to the end of its declarative region
- has an "actual scope" which may be smaller than its potential
scope because of the intrusion of other considerations, such as the
declaration of another variable of the same name at some point within
its declarative region
Variable/Object Scopes in C++
The different scopes for a variable or object in C++ are:
- local scope: declared inside a function or its parameter list
(This is sometimes called function scope, although this latter term
is sometimes restricted to apply only to "labels" defined within a
function.)
- global scope (or file scope): declared outside any function, and
possibly within the unnamed namespace of a file
- block scope: declared inside an anonymous block, i.e., inside
braces
- prototype scope: declared in the parameter list of a function
prototype
- class scope: declared inside a class
- namespace scope: declared inside a named namespace