c++ static/lexical scope
C++
是一个静态、强类型的编译型编程语言,变量的生命周期需要在编译器确定。这与动态语言是完全不同的。
Scoping is generally divided into two classes:
- Static Scoping (也称作
lexical scope
) - Dynamic Scoping
Static scoping is also called lexical scoping. In this scoping, a variable always refers to its top-level environment. This is a property of the program text and is unrelated to the run-time call stack. Static scoping also makes it much easier to make a modular code as a programmer can figure out the scope just by looking at the code. In contrast, dynamic scope requires the programmer to anticipate all possible dynamic contexts. In most programming languages including C, C++, and Java, variables are always statically (or lexically) scoped i.e., binding of a variable can be determined by program text and is independent of the run-time function call stack.
|
|
编译后,运行得的的结果是
|
|