曾经我认为这两个具有相同的含义,但在阅读更多相关内容后,我仍然不清楚它们之间的区别。局部范围有时不是指功能范围吗? 只有标签有函数作用域是什么意思?
最佳答案
void doSomething()
{ <-------
{ <---- |
| |
int a; Local Scope Function Scope
| |
} <---- |
} <-------
函数范围在外部 {
}
之间。
局部作用域在内部 {
}
之间
请注意,由 {``}
创建的任何范围都可以作为本地范围调用,而函数体开头的 {``}
创建功能范围。
因此,有时局部作用域可以与函数作用域相同。
what does it mean that only labels have a function scope?
Labels
只是标识符后跟一个冒号。带标签的语句用作 goto
语句的目标。标签可以在它们出现的函数中的任何地方使用,但不能在函数体之外引用。因此,据说它们具有功能范围。
代码示例:
int doSomething(int x, int y, int z)
{
label: x += (y + z); /* label has function scope*/
if (x > 1)
goto label;
}
int doSomethingMore(int a, int b, int c)
{
if (a > 1)
goto label; /* illegal jump to undefined label */
}
关于c++ - 局部作用域和函数作用域的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7933814/