通过输出参数,C ++编码标准返回结果 [英] Returning result via Output-parameter, c++ coding standard

查看:76
本文介绍了通过输出参数,C ++编码标准返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级中有一个成员函数,如下所示:

I have a member function in my class like this:

int MyClass::m_Func(int& val);

其中,我进行了一些运算并将结果放入 val 。根据操作的结果,我从函数中返回了不同的值。例如,如果成功,则返回 0 或其他值(如果发生任何错误)。

in which, I do some operation and put the result in val. And depending upon the result of the operation, I returned different values from the function. Like, if it is successful, I return 0 or other values if any error occurs.

我的一个朋友告诉我,将变量的引用传递给成员函数不是一种好习惯。是真的吗如果是,为什么?

One of my friend told me that it is not a good practice to pass a reference of a variable to a member function. Is it true? If yes, why so?

推荐答案

这种做法有些可疑的主要原因,以及为什么我通常会建议反对,就是用户在调用您的函数时

The main reason why this practice is somewhat suspicious, and why I'd generally recommend against it, is that users, when they call your function,

int val;
obj.my_Func(val);

可能不知道 val的值实际上已被修改。当函数只有一个参数且没有错误代码以外的其他返回值时,这可能或多或少是显而易见的,但是一旦您调用了一个稍微复杂的函数,例如

may not be aware of the fact that the value of val is in fact modified. This may be more or less obvious when the function has only one parameter and no return value other than an error code, but as soon as you have a slightly more complex function call, e.g.

int x,y,rad,len;
obj.calculate_coord(x,y,rad,len);

目的可能是计算 rad len 来自 x y ,反之亦然,或者完全不同的东西。由于参数名称是由用户选择的,并且它们的顺序或类型不提示任何参数作为输入参数,哪些参数作为输出参数,因此这可能会引起误解,从而使代码更难以维护并引起错误。

the intention may be to calculate rad and len from x and y, or vice versa, or something entirely different. Since the argument names are chosen by the user, and their order or types do not give any hint on which ones serve as input and which ones as output parameters, this can cause misunderstandings, hence make the code harder to maintain and give rise to bugs.

另一方面,使用C ++语法不仅返回值错误代码也很简单,而是一个或多个值错误代码的组合。

On the other hand, it is quite straight-forward to use C++ syntax to properly return not just the value or the error code, but a combination of one or more values and an error code.

在最简单的情况下,您可以使用这样的函数(使用C + +11语法):

In the simplest case you may use a function like this (using C++11 syntax):

std::tuple<int,int,int> my_Func(int input)
{
  /* calculate.. */

  return { error_code , val1 , val2 };
}

返回两个值和一个错误代码。毫无疑问, input 是纯输入参数(按值传递),返回值很复杂,并且包含您可能要返回的所有内容。

to return two values and an error code. There is no question that input is a pure input parameter (passed by value) and the return value is complex and includes everything you might want to return.

与某些程序员的建议相反,即使返回值更大(由于返回值的优化,在C ++ 11中,这也不会引起很多不必要的复制)移动语义)。

Contrary to what some programmers may suggest, this does not usually cause a lot of unnecessary copying, even when the return value is much larger (because of return value optimization, and in C++11 because of move semantics).

可能会有例外,尤其是在C ++ 11之前的版本中,即在特殊情况下,不能使用移动语义或出于某种原因返回价值优化不适用。在某些情况下,代码分析显示a)该函数的使用量非常大,并且b)出于某种原因,将输出通过引用传递的速度更快。在这些情况下,通过引用传递输出参数可能是使用的正确技术。

There may be exceptions, especially in pre-C++11, i.e. special cases where move semantics can't be used or for some reason return value optimization doesn't apply. There may also be cases where code profiling reveals that a) the function is used extremely heavily, and b) for whatever reason, passing the output through a reference is faster. In these cases, passing an output parameter by reference may be the right technique to use.

当然,我说的是上面的内容仅适用于将引用用作输出参数,即作为将值返回给用户的间接方式,尤其是当该函数修改其某些参数的事实因其名称而变得不明显时。

Of course, what I said above applies only to the use of references as output parameters, i.e. as an indirect way of returning values to the user, in particular when the fact that the function modifies some of its parameters isn't made obvious by its name.

还有很多其他有效的通过引用传递的理由,特别是。通过const-reference。

There can be many other, and valid, reasons for passing by reference, esp. by const-reference.

这篇关于通过输出参数,C ++编码标准返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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