gpt4 book ai didi

c++ - "local variables at the outermost scope of the function may not use the same name as any parameter"是什么意思?

转载 作者:IT老高 更新时间:2023-10-28 23:03:22 26 4
gpt4 key购买 nike

我一直在阅读 C++ 入门第 5 版。在第 6.1 章功能参数列表的第三段中。它写道“此外,函数最外层范围内的局部变量不得使用与任何参数相同的名称”。什么意思?

我不是以英语为母语的人。我不明白函数的“最外层范围”的实际含义。

最佳答案

函数的最外层是定义函数体的 block 。您可以将其他(内部) block 放入其中,并在该 block 的本地变量中声明变量。内部 block 中的变量可以与外部 block 中的变量或函数参数具有相同的名称;他们将名称隐藏在外部范围内。外部 block 中的变量不能与函数参数同名。

演示:

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
}

关于c++ - "local variables at the outermost scope of the function may not use the same name as any parameter"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30125671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com