mardi 11 juillet 2017

Return error value if an error occurs, otherwise, continue with function

Is there any way better way to write this in C++?

int func()
{
  int error = foo(); // can return true/false
  if (error != 0) // if an error is returned from foo(), pass it up to the caller of func()
    return error;

  // otherwise, proceed
  error = bar(); // can return true/false
  if (error != 0)
    return error;

  return error; // all calls succeeded, return 0.
}

Aucun commentaire:

Enregistrer un commentaire