Tuesday, March 24, 2009

Name spaces & Visibility

Name space is the scope within which an identifier must be unique. C uses four distinct classes of identifiers:
Structure, union, and enumeration tags. These must be unique within the block in which they are defined. Tags declared outside of any function must be unique.
.
goto label names. These must be unique within the function in which they are declared

Variables, typedefs, functions, and enumeration members. These must be unique within the scope in which they are defined. Externally declared identifiers must be unique among externally declared variables.
Structure and union member names. These must be unique within the structure or union in which they are defined. There is no restriction on the type or offset of members with the same member name in different structures

Visibility:
The visibility of an identifier is that region of the program source code from which legal access can be made to the identifier's associated object.

Scope and visibility usually coincide, though there are circumstances under which an object becomes temporarily hidden by the appearance of a duplicate identifier: the object still exists but the original identifier cannot be used to access it until the scope of the duplicate identifier is ended.

Note: Visibility cannot exceed scope, but scope can exceed visibility.

.

.
.
{
int i; char ch; // auto by default
i = 3; // int i and char ch in scope and visible
.
.
.

{
double i;
i = 3.0e3; // double i in scope and visible
// int i=3 in scope but hidden
ch = 'A'; // char ch in scope and visible
}
// double i out of scope
i += 1; // int i visible and = 4
.
.
.
// char ch still in scope & visible = 'A'

}
.
.
.

// int i and char ch out of scope

Again, special rules apply to hidden class names and class member names: C++ operators allow hidden identifiers to be accessed under certain conditions

No comments:

Post a Comment