【发布时间】:2013-12-27 22:44:27
【问题描述】:
对不起,如果这个问题不适合 SO。
我有一个类似于下面给出的 MyFun() 的 C++ 函数。
从这个函数中,我调用了一些(比如大约 30 个)返回布尔变量的其他函数(true 表示成功,false 表示失败)。如果这些函数中的任何一个返回 false,我也必须从 MyFun() 返回 false。另外,如果中间函数调用失败,我不应该立即退出(不调用其余函数)。
目前我正在这样做,如下所示,但感觉可能有一种更简洁/更简洁的方式来处理这个问题。任何建议表示赞赏。
非常感谢。
bool MyFun() // fn that returns false on failure
{
bool Result = true;
if (false == AnotherFn1()) // Another fn that returns false on failure
{
Result = false;
}
if (false == AnotherFn2()) // Another fn that returns false on failure
{
Result = false;
}
// Repeat this a number of times.
.
.
.
if (false == Result)
{
cout << "Some function call failed";
}
return Result;
}
【问题讨论】:
-
Sorry if this question is suited for SO上帝我知道我讨厌这种情况发生 -
@LightnessRacesinOrbit : 错字........ :)
-
这个问题似乎是题外话,因为它属于codereview.stackexchange.com
-
@stefan:你真的建议过这个吗?抱歉,我对 SO 不太熟悉。
-
@NeonGlow:公平地说,这是真的。
标签: c++