Programming in C++ Name Precedence (or Name Hiding) %when a function declares a local identifier with the same name as a global identifier, the local identifier takes precedence within that function
6 Name Precedence (or Name Hiding) ❖when a function declares a local identifier with the same name as a global identifier, the local identifier takes precedence within that function
Programming in C++ Detailed Scope Rules 1 Function name has global scope 2 Function parameter scope is identical to scope of a local variable declared in the outermost block of the function body 3 Global variable(or constant) scope extends from declaration to the end of the file, except as noted in rule 5 4 Local variable (or constant) scope extends from declaration to the end of the block where declared This scope includes any nested blocks, except as noted in rule 5 5 An identifier's scope does not include any nested block that contains a locally declared identifier with the same name (local identifiers have name precedence)
7 Detailed Scope Rules 1 Function name has global scope. 2 Function parameter scope is identical to scope of a local variable declared in the outermost block of the function body. 3 Global variable (or constant) scope extends from declaration to the end of the file, except as noted in rule 5. 4 Local variable (or constant) scope extends from declaration to the end of the block where declared. This scope includes any nested blocks, except as noted in rule 5. 5 An identifier’s scope does not include any nested block that contains a locally declared identifier with the same name (local identifiers have name precedence)
Programming in C++ Figure Scope Diagram for Scope Rules Program int a1 char a2. int main() void block1( int a1 char& b2) int c1 int d2, void block2() int a1 int b2. while(-) ∥/ Block3 C1 int b2
8 Figure Scope Diagram for Scope Rules Program int a1; char a2; int main ( ) { } void block1 ( { } } void block2 ( ) { int a1, char& b2 ) int c1; int d2; int a1; int b2; while (…) { //Block3 } int c1; int b2;
Programming in C++ Name Precedence Implemented by Compiler Determines Scope . When an expression refers to an identifier, the compiler first checks the local declarations s If the identifier isn't local, compiler works outward through each level of nesting until it finds an identifier with same name. There it stops o Any identifier with the same name declared at a level further out is never reached o If compiler reaches global declarations and still can't find the identifier, an error message results
9 Name Precedence Implemented by Compiler Determines Scope ❖ When an expression refers to an identifier, the compiler first checks the local declarations. ❖ If the identifier isn’t local, compiler works outward through each level of nesting until it finds an identifier with same name. There it stops. ❖ Any identifier with the same name declared at a level further out is never reached. ❖ If compiler reaches global declarations and still can’t find the identifier, an error message results
Programming in C++ Namespace Scope o the scope of an identifier declared in a namespace definition extends from the point of declaration to the end of the namespace body, and its scope includes the scope of a using directive specifying that namespace 10
10 Namespace Scope ❖the scope of an identifier declared in a namespace definition extends from the point of declaration to the end of the namespace body, and its scope includes the scope of a using directive specifying that namespace