C++: local scope

Yao Yao on April 12, 2015
  • Published in category
  • C++

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
}


blog comments powered by Disqus