如何从C ++函数返回多个错误代码 [英] how to return multiple error codes from C++ function

查看:162
本文介绍了如何从C ++函数返回多个错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C ++函数返回成功或一个或多个错误代码的好方法是什么?



我有一个名为save()的成员函数,每个成员变量,有至少十个这些成员变量保存到,为了调用save(),我想知道调用是否失败,如果是,在哪个成员变量(一些是


解决方案

您可以返回具有多个错误字段的对象,也可以使用'out '参数。



这样做取决于你的设计和你想要返回什么。一个常见的情况是当你需要报告状态代码和排序的消息。这有时在函数返回状态代码作为返回值,然后通过out参数返回消息状态。



如果你只是返回一个集合的'代码',它可能更有意义构造一个结构类型并返回。在这种情况下,我将很容易作为一个out参数传递它,并让方法在内部更新它,而不是每次分配一个。



您打算这么做还是多次?


What is a good way to return success or one or more error codes from a C++ function?

I have this member function called save(), which saves to each of the member variables, there are at least ten of these member variables that are saved-to, for the call to save(), I want to find out if the call failed, and if so, on which member variable (some are hard failures, some are soft).

解决方案

You can either return an object that has multiple error fields or you can use 'out'parameters.

How you do this depends on your design and what exactly you are trying to return back. A common scenario is when you need to report back a status code along with a message of sorts. This is sometimes done where the function returns the status code as the return value and then returns the message status via an 'out' parameter.

If you are simply returning a set of 'codes', it might make more sense to construct a struct type and return that. In that case, I would be prone to pass it in as an out parameter and have the method internally update it instead of allocating a new one each time.

Are you planning on doing this once or many times?

这篇关于如何从C ++函数返回多个错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆