I have been reading the C++ primer 5th edition. In the third paragraph of Function Parameter List of Chapter 6.1 . It writes "Moreover, local variables at the outermost scope of the function may not use the same name as any parameter". What does it mean?
我一直在阅读C ++入门第5版。在第6.1章的函数参数列表的第三段中。它写道“此外,函数最外层的局部变量可能不会使用与任何参数相同的名称”。这是什么意思?
I am not native English speaker. I don't understand the actual meanings of "outermost scope" of the function.
我不是母语为英语的人。我不明白函数“最外层范围”的实际含义。
5 个解决方案
#1
The outermost scope of the function is the block that defines the function's body. You can put other (inner) blocks inside that, and declare variables in those which are local to that block. Variables in inner blocks can have the same name as those in an outer block, or the function parameters; they hide the names in the outer scope. Variables in the outer block can't have the same name as a function parameter.
函数的最外层是定义函数体的块。您可以将其他(内部)块放在其中,并在该块的本地变量中声明变量。内部块中的变量可以与外部块中的变量或函数参数具有相同的名称;他们将名称隐藏在外部范围内。外部块中的变量不能与函数参数同名。
To demonstrate:
void f(int a) // function has a parameter
{ // beginning of function scope
int b; // OK: local variable
{ // beginning of inner block
int a; // OK: hides parameter
int b; // OK: hides outer variable
} // end of inner block
int a; // Error: can't have same name as parameter
}
#2
It means you can't do things like this:
这意味着你不能做这样的事情:
void foo (int x)
{
int x = 4; //in the outermost scope, invalid
}
You can, however, do this:
但是,您可以这样做:
void foo (int x)
{
{ //this introduces a new scope
int x = 4; //not in the outermost scope, valid
}
}
#3
It means that this is invalid:
这意味着这是无效的:
void f(int p)
{
int p = 3;
}
wheras this
void f(int p)
{
{
int p = 3;
}
}
is allowed.
#4
This rule is valid not only for function parameters of function definitions but also for iteration and conditional statements and for exceptions handlers
此规则不仅适用于函数定义的函数参数,还适用于迭代和条件语句以及异常处理程序
3.3.3 Block scope
3.3.3块范围
2 The potential scope of a function parameter name (including one appearing in a lambda-declarator) or of a function-local predefined variable in a function definition (8.4) begins at its point of declaration. If the function has a function-try-block the potential scope of a parameter or of a function-local predefined variable ends at the end of the last associated handler, otherwise it ends at the end of the outermost block of the function definition. A parameter name shall not be redeclared in the outermost block of the function definition nor in the outermost block of any handler associated with a function-try-block.
2函数参数名称(包括一个出现在lambda声明符中)或函数定义中的函数局部预定义变量(8.4)的潜在范围从其声明点开始。如果函数具有函数try-block,则参数或函数本地预定义变量的潜在范围在最后一个关联的处理程序的末尾结束,否则它在函数定义的最外面的块的末尾结束。参数名称不应在函数定义的最外层块中重新声明,也不得在与函数try-block相关联的任何处理程序的最外层块中重新声明。
3 The name declared in an exception-declaration is local to the handler and shall not be redeclared in the outermost block of the handler.
3在异常声明中声明的名称是处理程序的本地名称,不应在处理程序的最外层块中重新声明。
4 Names declared in the for-init-statement, the for-range-declaration, and in the condition of if, while, for, and switch statements are local to the if, while, for, or switch statement (including the controlled statement), and shall not be redeclared in a subsequent condition of that statement nor in the outermost block (or, for the if statement, any of the outermost blocks) of the controlled statement; see 6.4.
4在for-init-statement,for-range-declaration以及if,while,for和switch语句的条件中声明的名称是if,while,for或switch语句的本地名称(包括受控语句) ),并且不得在该陈述的后续条件中,也不得在受控陈述的最外层(或if语句,任何最外层)中重新宣布;见6.4。
For example this code snippet is invalid
例如,此代码段无效
if( int x = SomeFunction() )
{
int x; // invalid declaration
//...
}
#5
Imagine that you have a function 'foo' that takes an integer parameter 'bar'.
想象一下,你有一个带有整数参数'bar'的函数'foo'。
int foo (int bar)
{
int bar = 0; // < illegal, this is the 'outermost' scope
if (bar == 10) {
int bar = 5; // legal (though unadvisable) this 'shadows' the passed-in 'bar'
return bar;
}
return bar;
}
The first internal declaration of 'bar' is illegal, since the passed in parameter is also declared in the same context (even though the syntax doesn't necessarily make that clear.)
'bar'的第一个内部声明是非法的,因为传入的参数也在同一个上下文中声明(即使语法不一定清楚。)
Just as it would be incorrect to write:
就像写不正确一样:
int bar;
char bar[10];
Since those two variables share the same scope.
由于这两个变量共享相同的范围。
The second declaration of bar (in the function foo above) is legal (though usually a bad idea) since it is declared in the inner scope of the 'if' and therefore fair game.
bar的第二个声明(在上面的函数foo中)是合法的(虽然通常是一个坏主意),因为它是在'if'因此公平游戏的内部范围内声明的。
Hope that helps.
希望有所帮助。
#1
The outermost scope of the function is the block that defines the function's body. You can put other (inner) blocks inside that, and declare variables in those which are local to that block. Variables in inner blocks can have the same name as those in an outer block, or the function parameters; they hide the names in the outer scope. Variables in the outer block can't have the same name as a function parameter.
函数的最外层是定义函数体的块。您可以将其他(内部)块放在其中,并在该块的本地变量中声明变量。内部块中的变量可以与外部块中的变量或函数参数具有相同的名称;他们将名称隐藏在外部范围内。外部块中的变量不能与函数参数同名。
To demonstrate:
void f(int a) // function has a parameter
{ // beginning of function scope
int b; // OK: local variable
{ // beginning of inner block
int a; // OK: hides parameter
int b; // OK: hides outer variable
} // end of inner block
int a; // Error: can't have same name as parameter
}
#2
It means you can't do things like this:
这意味着你不能做这样的事情:
void foo (int x)
{
int x = 4; //in the outermost scope, invalid
}
You can, however, do this:
但是,您可以这样做:
void foo (int x)
{
{ //this introduces a new scope
int x = 4; //not in the outermost scope, valid
}
}
#3
It means that this is invalid:
这意味着这是无效的:
void f(int p)
{
int p = 3;
}
wheras this
void f(int p)
{
{
int p = 3;
}
}
is allowed.
#4
This rule is valid not only for function parameters of function definitions but also for iteration and conditional statements and for exceptions handlers
此规则不仅适用于函数定义的函数参数,还适用于迭代和条件语句以及异常处理程序
3.3.3 Block scope
3.3.3块范围
2 The potential scope of a function parameter name (including one appearing in a lambda-declarator) or of a function-local predefined variable in a function definition (8.4) begins at its point of declaration. If the function has a function-try-block the potential scope of a parameter or of a function-local predefined variable ends at the end of the last associated handler, otherwise it ends at the end of the outermost block of the function definition. A parameter name shall not be redeclared in the outermost block of the function definition nor in the outermost block of any handler associated with a function-try-block.
2函数参数名称(包括一个出现在lambda声明符中)或函数定义中的函数局部预定义变量(8.4)的潜在范围从其声明点开始。如果函数具有函数try-block,则参数或函数本地预定义变量的潜在范围在最后一个关联的处理程序的末尾结束,否则它在函数定义的最外面的块的末尾结束。参数名称不应在函数定义的最外层块中重新声明,也不得在与函数try-block相关联的任何处理程序的最外层块中重新声明。
3 The name declared in an exception-declaration is local to the handler and shall not be redeclared in the outermost block of the handler.
3在异常声明中声明的名称是处理程序的本地名称,不应在处理程序的最外层块中重新声明。
4 Names declared in the for-init-statement, the for-range-declaration, and in the condition of if, while, for, and switch statements are local to the if, while, for, or switch statement (including the controlled statement), and shall not be redeclared in a subsequent condition of that statement nor in the outermost block (or, for the if statement, any of the outermost blocks) of the controlled statement; see 6.4.
4在for-init-statement,for-range-declaration以及if,while,for和switch语句的条件中声明的名称是if,while,for或switch语句的本地名称(包括受控语句) ),并且不得在该陈述的后续条件中,也不得在受控陈述的最外层(或if语句,任何最外层)中重新宣布;见6.4。
For example this code snippet is invalid
例如,此代码段无效
if( int x = SomeFunction() )
{
int x; // invalid declaration
//...
}
#5
Imagine that you have a function 'foo' that takes an integer parameter 'bar'.
想象一下,你有一个带有整数参数'bar'的函数'foo'。
int foo (int bar)
{
int bar = 0; // < illegal, this is the 'outermost' scope
if (bar == 10) {
int bar = 5; // legal (though unadvisable) this 'shadows' the passed-in 'bar'
return bar;
}
return bar;
}
The first internal declaration of 'bar' is illegal, since the passed in parameter is also declared in the same context (even though the syntax doesn't necessarily make that clear.)
'bar'的第一个内部声明是非法的,因为传入的参数也在同一个上下文中声明(即使语法不一定清楚。)
Just as it would be incorrect to write:
就像写不正确一样:
int bar;
char bar[10];
Since those two variables share the same scope.
由于这两个变量共享相同的范围。
The second declaration of bar (in the function foo above) is legal (though usually a bad idea) since it is declared in the inner scope of the 'if' and therefore fair game.
bar的第二个声明(在上面的函数foo中)是合法的(虽然通常是一个坏主意),因为它是在'if'因此公平游戏的内部范围内声明的。
Hope that helps.
希望有所帮助。