gpt4 book ai didi

c++ - 在已经是参数名称的 C++ 函数中定义变量

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:20 24 4
gpt4 key购买 nike

在 C++ 中,如果您在函数体中定义一个与函数参数之一同名的变量,会发生什么情况?

例如:

static void f(int i)
{
int i;

...

}

i 函数内部的定义有什么作用吗?

它会造成任何伤害吗?

您有什么好的理由可以想到以这种方式编写函数吗?

最佳答案

根据C++标准

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.

在这种情况下,编译器应发出诊断消息。

关于c++ - 在已经是参数名称的 C++ 函数中定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25432956/

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