传递对函数的引用
passing reference to function
我有一段代码执行类似以下示例的操作,但我不确定这是否正确,因为可执行文件按预期运行。
// source.cpp
void compute_x(int& ref)
{
ref = 0;
}
void f(int x)
{
int local = x;
local = 1;
if (local)
{
return copute_x(local);
}
else return;
}
int main()
{
f(2);
return 0;
}
代码运行但是,变量 local
有效一次 f
returns?
变量 local
在 f
return 秒后超出范围。
在您 编辑 之后:但是 return 值是从函数 returned 并且随后从 main
ed returned ].
No.the 局部变量无效,因为它的作用域在 f 函数定义的花括号之间……之后它就不存在了
我有一段代码执行类似以下示例的操作,但我不确定这是否正确,因为可执行文件按预期运行。
// source.cpp
void compute_x(int& ref)
{
ref = 0;
}
void f(int x)
{
int local = x;
local = 1;
if (local)
{
return copute_x(local);
}
else return;
}
int main()
{
f(2);
return 0;
}
代码运行但是,变量 local
有效一次 f
returns?
变量 local
在 f
return 秒后超出范围。
在您 编辑 之后:但是 return 值是从函数 returned 并且随后从 main
ed returned ].
No.the 局部变量无效,因为它的作用域在 f 函数定义的花括号之间……之后它就不存在了