Name resolution (programming languages): Difference between revisions

Content deleted Content added
No edit summary
Name masking: Disambiguated and clarified the code example greatly.
Line 39:
The outer variable X is said to be ''shadowed'' by the inner variable X'.
 
For example, the parameter x"foo" masksshadows the local variable "foo" in this common pattern:
<source lang=java>
private int foo; // A declaration with nameName "foo" is declared in anthe outer scope
 
public void setFoo(int foo) { // A declaration with the same name in the inner scope
public void setFoo(int foo) { // Name "foo" is resolved by lookingdeclared in the innermostinner scope first,
// so the author uses a different syntax, this.foo, to refer to the name "foo"
// in the outer scope.
this.foo = foo;
}
// "foo" here means the same as this.foo below,
// since setFoo's parameter is no longer in scope.
public int getFoo() { return foo; }
</source>
 
public int getFoo() { return foo; }
return foo;
}
</source>
Name masking can cause [[Function overloading#Complications|complications in function overloading]], due to overloading not happening across scopes in some languages, notably C++, thus requiring all overloaded functions to be redeclared or explicitly imported into a given namespace.