C++: local scope
按 Difference between local scope and function scope 的说法:
void doSomething()
{ // <-------|
{ // <---| |
// | |
int a; // Local Scope Function Scope
// | |
} // <---| |
} // <-------|
- Function Scope is between outer
{ }
. - Local scope is between inner
{ }
.
Thinking in C++ 上有一个有点无耻的用法:
int main() {
{
// experiment 1
} // at the end of local scope, variables in experiment 1 are all destructed
// 留下一个全新的环境给 experiment 2
// experiment 2
}
Comments