将int重新分配给const int?

4 c const

我正在阅读C代码库,我发现了一个看起来像这样的代码片段:

void foo(int bar) {
    const int _bar = bar;
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后,作者在其余代码中使用_bar.为什么这样做?这是优化,还是有其他原因?

Mah*_*esh 6

然后,作者在其余代码中使用_bar.

如果_bar通过out而不使用函数参数,我将通过函数参数限定const.

void foo( const int bar )
{
     // use bar but modifications to bar itself are not allowed.
}
Run Code Online (Sandbox Code Playgroud)