局部作用域和函数作用域的区别
Difference between local scope and function scope
一旦我认为这两个有相同的意思,但在阅读了更多关于它我仍然不清楚的区别。局部作用域有时不是指函数的作用域吗?只有标签有函数作用域是什么意思?
void doSomething()
{ <-------
{ <---- |
| |
int a; Local Scope Function Scope
| |
} <---- |
} <-------
Function Scope between outer {
}
局部作用域在内部{
}
之间
注意,{``}
创建的任何作用域都可以作为局部作用域调用,而函数体开头的{``}
创建的是function作用域。
因此,有时局部作用域可以与函数作用域相同。
只有标签有函数作用域是什么意思?
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 */
}
局部作用域是一个{和它的关闭}之间的区域。函数作用域是函数的开始{和结束}之间的区域,它可以包含更多的"局部"作用域。标签在定义它的整个函数中是可见的,例如
int f( int a )
{
int b = 8;
if ( a > 14 )
{
int c = 50;
label:
return c - a - b;
}
if ( a > 7 ) goto label;
return -99;
}
int c
在其封闭块外不可见。标签在其封闭块之外可见,但仅在函数范围内可见。
局部作用域有时不是指函数作用域吗?
是的。在大多数c派生语言中,变量在声明它们的范围内是有效的。如果在函数内部声明变量,而不是在任何其他代码块中声明变量,则该变量通常称为"局部"或"自动"变量。你可以在函数的任何地方引用它。另一方面,如果在另一个代码块中声明变量——比如在条件语句体中,那么该变量仅在该代码块中有效。这里还有几个很好的例子。
,只有标签有函数作用域是什么意思?
Context会有帮助,但这意味着你不能从一个函数跳转到另一个函数中的标签。
void foo(int a) {
if (a == 0) goto here; // okay -- 'here' is inside this function
printf("a is not zeron");
goto there; // not okay -- 'there' is not inside this function
here:
return;
}
void bar(int b) {
if (b == 0) goto there; // okay -- 'there' is in this function
printf("b is not zeron");
there:
return;
}
不是要捅马蜂窝,但标签的范围可能不会经常出现。标签主要用于goto
语句,它很少需要,即使您选择使用goto
,您可能甚至不会想到尝试跳转到另一个函数。
函数的作用域略大于函数体的作用域:函数的实参在外部作用域,而局部变量只在内部作用域。这在function-try-block中最为明显:
void f(int a) try {
// function body
} catch(...) {
// catch block
}
在catch块中,只有函数作用域中的变量仍在作用域中,而局部变量不在。
当然,你也可以并且确实一直引入更深层次的嵌套作用域,例如在for
循环体或条件体中。
bool m[3][3];
void f1()
{
int i;
// redefining a variable with the same name in the same scope isn't possible
//int i; //error C2086: 'int i' : redefinition
}
void f2()
{
int i; // ok, same name as the i in f1(), different function scope.
{
int i; // ok, same name as the i above, but different local scope.
}
// the scope if the following i is local to the for loop, so it's ok, too.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (m[i][j])
goto loopExit;
}
}
loopExit:
std::cout << "done checking m";
// redefining a label with the same name in the same function isn't possible
// loopExit:; // error C2045: 'loopExit' : label redefined
}
void f3()
{
loopExit:; // ok, same label name as in f2(), but different function scope
}
- 未在作用域中声明unordered_map
- 有没有一种方法可以在编译时获得作用域类名
- C++quit()函数中可能存在作用域问题
- 未在此作用域OpenCV3.4中声明cvSaveImage
- 全局作用域中函数指针的赋值
- 在类函数中初始化外部作用域变量
- 不同作用域中的静态变量和全局变量
- 是同一作用域的函数部分中的函数调用
- 未在此作用域中声明的函数和变量 (C++)
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 将作用域枚举转换为基础类型
- 表达式必须具有完整或无作用域的枚举图
- 在构造函数中输入对象时C++类成员作用域
- 无法让"std::enable_if"适用于无作用域枚举
- 为什么不能直接引用作用域枚举类成员,而不能为无作用域枚举生成类成员?
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 函数和类的作用域有什么区别
- c++中的匿名命名空间和匿名作用域之间有什么区别
- 局部作用域和函数作用域的区别