IN/OUT 参数以及如何在 C++ 中使用它们

发布于 2024-11-27 11:01:42 字数 386 浏览 0 评论 0原文

当阅读有关来自不同类型的外部库的函数的文档时,我总是看到文档声明变量必须是[IN/OUT]。有人可以让我详细了解 [IN/OUT] 与通过引用或按值传递的函数参数之间的关系吗?

这是我遇到的一个函数示例,它告诉我它需要一个 [IN/OUT] 参数:

原型: ULONG GetActivationState( ULONG * pActivationState );

参数

  • 类型: ULONG*
  • 变量:pActivationState
  • 模式:IN/OUT

When reading documentation on functions from external libraries of different kinds I have always seen the documentation state that a variable has to be [IN/OUT]. Could someone give me a detailed understanding on how [IN/OUT] relates to parameters of a function being passed by reference or by value.

Here is an example of a function I have come across that tells me it needs an [IN/OUT] parameter:

Prototype:
ULONG GetActivationState( ULONG * pActivationState );

Parameters

  • Type: ULONG*
  • Variable: pActivationState
  • Mode: IN/OUT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一个人的旅程 2024-12-04 11:01:42

这部分适用于所有类型的参数 - 大多数库接口都尝试与 C 兼容,因此通过指针而不是通过引用传递参数更常见。

IN:当参数列为 IN 时,接口保证不会修改该参数。在我看来,通过将参数标记为 const 可以更好地传达这一点,然后语言本身将阻止对该值的修改。如果此参数按值传递,则无论在文档中将其标记为 IN(还是在原型中标记为 const)都无关紧要,因为该参数无论如何都是函数的本地参数。但为了避免复制,可以通过引用或指针传递它,在这种情况下 const 关键字就变得非常重要。

OUT: 标记为 OUT 的参数通常意味着该参数的值在传递给函数时并不重要。事实上,如果它是通过指针传递的,甚至可能要求它为NULL,并且该函数将分配内存并将一个值传递回给您。

IN/OUT: IN/OUT 参数通常表示输入和输出值都有意义的内容。例如,如果您有一个填充缓冲区的库函数,它可能需要您传递一个指向缓冲区的指针,以及另一个指示缓冲区长度的指针。当函数返回时,第二个指针可能包含已写入缓冲区的实际字节数。

This part applies to all types of paramters - most library interfaces try to be C compatible, so it is more common to pass parameters by pointer, rather than by reference.

IN: When a parameter is listed as IN it is a guarantee being offered by the interface that it won't modify that parameter. In my opinion, this is better conveyed by marking the parameter as const, then the language itself will prevent modifications to the value. If this parameter is being passed by value, it is inconsequential whether it is marked IN in the documentation (or const in the prototype) since the parameter is local to the function anyway. But to avoid copying it may be passed by reference or by pointer, in which case the const keyword becomes very important.

OUT: A parameter marked OUT usually means that the value of the parameter when it is being passed to the function is not of any importance. In fact, if it being passed by pointer, it may even be required to be NULL, and the function will allocate memory and pass a value back to you.

IN/OUT: An IN/OUT parameter usually indicates something where both the input and output values are meaningful. For instance, if you have a library function that fills a buffer, it may require you to pass a pointer to the buffer, along with another pointer indicating the length of the buffer. When the function returns, the second pointer may contain the actual number of bytes that have been written to the buffer.

空名 2024-12-04 11:01:42

此参数是 in/out,因为您提供了一个在函数内部使用的值,并且函数会修改它以通知您函数内部发生的事情。该函数的用法如下:

ULONG activationState = 1; // example value
ULONG result = GetActivationState(&activationState);

请注意,您必须提供变量的地址,以便函数可以获取该值并在函数外部设置该值。例如,GetActivationState 函数可以执行如下操作:

ULONG GetActivationState(ULONG* pActivationState)
{
    if (*pActivationState == 1)
    {
    // do something
    // and inform by the modification of the variable, say, resetting it to 0
       *pActivationState = 0;
    }
    // ...
    return *pActivationState; // just an example, returns the same value
}

注意:

  1. 该函数接受参数作为指向 UINT 的非常量指针。这意味着它可能修改它。
  2. 该函数可以通过取消引用来访问您赋予参数的值。
  3. 该函数可以通过取消引用来再次修改该参数。
  4. 调用函数会看到 activationState 变量保存有new 值(本例中为 0)。

这是“按引用传递”的示例,它是通过使用 C 中的指针(以及 C++ 中的引用)来执行的。

This parameter is in/out because you provide a value that is used inside the function, and the function modifies it to inform you about something that happened inside the function. The usage of this function would be something like this:

ULONG activationState = 1; // example value
ULONG result = GetActivationState(&activationState);

note that you have to supply the address of the variable so that the function can get the value and set the value outside the function. For instance, the GetActivationState function can perform something like this:

ULONG GetActivationState(ULONG* pActivationState)
{
    if (*pActivationState == 1)
    {
    // do something
    // and inform by the modification of the variable, say, resetting it to 0
       *pActivationState = 0;
    }
    // ...
    return *pActivationState; // just an example, returns the same value
}

Note how:

  1. The function accepts the parameter as a non-const pointer to an UINT. This means it may modify it.
  2. The function can access the value you gave to the parameter by dereferencing it
  3. The function can modify the parameter again by dereferencing it.
  4. The calling function sees the activationState variable holding the new value (0 in this case).

This is an example of "pass by reference", which is performed by using pointers in C (and also with references in C++.)

夜灵血窟げ 2024-12-04 11:01:42

通常,标记为 IN/OUT 的内容将通过非常量指针或引用传递,允许函数直接修改变量以及读取变量。请务必检查文档,了解在传递该值之前是否需要设置该值。

标记为 IN 的参数将按值传递,或者通过常量指针或常量引用传递,从而不允许函数修改变量。

C++ 不强制仅使用 OUT 参数,但通常会使用非常量指针或引用来传递它们,类似于 IN/OUT。

Generally, things marked as IN/OUT will be passed via a non-const pointer or reference, allowing the function to modify the variable directly, as well as read it. Be sure to check the documentation to see if it expects the value to be set prior to passing it in.

Parameters marked as IN will be passed by value, or by constant pointer or constant reference, disallowing the function from modifying the variable.

C++ doesn't enforce OUT-only parameters, but generally they will be passed using non-const pointer or references, similar to IN/OUT.

梦过后 2024-12-04 11:01:42

如果参数是 OUT,则必须通过引用传递。如果复制成本太高,纯 IN 参数通常会按值或 const 引用传递(没有什么可以阻止设计者通过引用传递它,但恕我直言,这不是很好的设计)。 IN/OUT 参数必须通过引用传递。

If a parameter is OUT, it has to be passed by reference. A purely IN parameter would be usually passed by value or const reference, if the cost of copying is too high (nothing prevents the designed from passing it by reference, but it's not very good design IMHO). An IN/OUT parameter must be passed by reference.

哥,最终变帅啦 2024-12-04 11:01:42

对于 in、out 和 in/out 的使用,我的看法很复杂。

好处:如果做得正确,它可以向文档的读者传达意图。

缺点:很多时候都没有正确完成。这些名称显然不是该语言的一部分;它们是语言的一部分。它们要么在注释中,要么在与代码分开维护的某些文档中。我见过太多参数被标记为“out”的情况,但在代码中使用该参数完成的第一件事就是将其用作右侧值。

I'm of a mixed mind regarding the use of in, out, and in/out.

Upside: When done properly, it communicates intent to the reader of the documentation.

Downside: Far too often it is not done properly. Those designations obviously are not a part of the language; they are either in comments or are in some document that is maintained separately from the code. I've seen far too many cases where a parameter was marked as "out" but the first thing done in the code with that parameter is to use it as a right-hand side value.

韬韬不绝 2024-12-04 11:01:42

您可以按值(简单类型)或常量引用 const & 来使用仅输入参数。使用非常量引用&或指针*作为输入/输出参数来更改变量的值。您还可以使用指针引用 * & 来更改实际指针指向的地址(输入/输出)。正如 Dave Smith 指出的那样,C++ 中没有 out only 参数。

You can use by value (simply types) or by constant reference const & for input only parameters. Use non-const reference & or pointer * as in/out parameter to change the value of the variable. You can also use a pointer reference * & to allow you to change the address the actual pointer points to (in/out). As Dave Smith pointed out there is no out only parameter in C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文