问题描述
在某些函数中,我需要在
多个地方返回多个错误代码,我使用多个return语句。比如说。
if(Found == 1)
{
if(val == -1)
返回error1;
}
else
{
if(val2 == -1)
返回error2;
}
返回成功;
这只是一个简单的例子。我的案例有很多有条件的
语句。
另一种方法是使用goto语句。让我知道
更好的方法,这是更好的选择(多次返回
语句或Goto')
谢谢,
In some functions where i need to return multiple error codes at
multiple places, I use multiple return statements. Say for ex.
if (Found == 1)
{
if (val == -1)
return error1;
}
else
{
if (val2 == -1)
return error2;
}
return success;
This is just a simple example. My case have lot of conditional
statements.
the alternate approach is to use goto statements. Let me know the
better way of doing this and which is preferrable (multiple return
statements or Goto''s)
Thanks,
推荐答案
使用gotos,你可以让所有出口点跳到函数末尾附近的公共点
,这对于放置常见的清理代码或者/>
调试语句。如果你有多个返回值,你需要多个
goto标签。
With gotos, you can make all the exit points jump to a common point near
the end of the function, which is useful to place common clean-up code or
debug statements. If you have multiple return values, you''ll need multiple
goto labels.
首选多个''return''语句而不是''goto''。
-vs_p ......
Hi,
Prefer multiple ''return'' statements than ''goto''.
-vs_p...
这篇关于多个返回语句与转到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!