7.7 SCOPE OF AN IDENTIFIER IN C


7.7 SCOPE OF AN IDENTIFIER IN C++

A declaration introduces an identifier into a scope, meaning that after the declaration the identifier can only be used in a specific part of the program. The five scopes of an identifier are

  • namespace scope

  • file scope

  • block scope

  • function scope

  • function-prototype scope

We have already discussed in detail the namespace scope in Chapter 3. To quickly summarize the discussion in that chapter: (a) The identifiers that are the members of a namespace can be accessed outside the namespace through their namespace-qualified names. (b) Using-directives and using-declarations are available for convenience; with these a namespace member can be invoked without the use of the scope operator. (c) Namespaces can import other namespaces through the mechanism of using-directives and using-declarations. (d) Namespaces can be nested. (e) Unwieldy namespace names can be aliased to more convenient names. (f) Unnamed namespaces allow us to declare identifiers with internal linkage. And, (g) Koenig lookup allows an unqualified function name to be accessed if its argument type is defined in that namespace.

The scope of a global identifier-that is an identifier which is defined outside any function, class, or namespace-extends from the point of declaration to the end of the file in which its declaration occurs. Therefore, if you declare a function prototype at the beginning of a program file outside of any other function, the name of the function will be recognized everywhere from the point of the declaration and until the end of the file. Similarly, if you define a global variable, the name of the variable will be "known" until the end of the file. This is referred to as file scope.

The scope of a local identifier, which is an identifier declared inside a block, starts at the point of declaration and ends with the end of the block. Recall that a block is a section of code delimited by a {} pair. This is referred to as block scope. Local variables and function parameters have block scope. Note that an identifier can be declared at any point in a C++ program.

The fact that an identifier can be declared at any point in a C++ program leads naturally to the following question concerning nested blocks: What happens when an inner block contains an identifier declaration with the same name as one in the enclosing block? In such situations, the inner block declaration hides, or takes precedence over, the declaration in the enclosing block.[8] When an already declared identifier is redefined inside a block so as to refer to a different entity within the block, after exit from the block the identifier resumes its previous meaning. This is illustrated by the following example:

 
//HideScope.cc int x; // global x void f() { int x; // local x hides g lobal x x = 1; // assigns to local x { int x; // hides first local x x = 2; // assigns to second local x ::x = 20; // assigns to global x } x = 3; // assigns to first local x } int main() { int* p = &x; // take address of global x return 0; }

Although it may seem that hiding names is unavoidable when writing large programs, it is known to be a source of hard to locate bugs. That is because in a visual scan of a program it can often be difficult to determine as to which meaning of a variable one is referring to in such situations.

That brings us to the last two of the different types of scopes listed at the beginning of this section. The only identifiers with function scope are labels. Labels, recognized by the colon that follows them, are used mainly for marking the destination of goto statements. Another example consists of case labels used in switch structures. With regard to the function-prototype scope, as its name would suggest, the only identifiers that have this scope are the parameters used in function prototypes. As the reader should know from his or her C programming experience, it is not really necessary to name the parameters in a function prototype; only the types of the parameters need to be specified. However, it is common to include parameter names in function prototypes to make the code more readable by humans. The compiler ignores such names.

[8]By the same token, if an identifier inside a block has the same name as a global identifier, the former will hide the latter until the end of the block.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net