指针到指针与引用到指针之间的区别 (C++)

发布于 2024-12-20 08:39:45 字数 501 浏览 2 评论 0原文

我有一些使用接口指针的 COM 代码。代码的原作者实现了返回接口指针的函数,如下所示:

HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject* &pEnumerator ); // (1)

而不是传统的

HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject** ppEnumerator ); // (2)

函数 (1) 被这样调用:

hRes = Query ( sQuery, pEnumerator ); // (3)

这绝对看起来是错误的,但它工作正常。我不确定我是否只是选择这一行,因为 out 参数不是指向输出变量的指针,或者因为这种方法有问题。

对于输出参数使用指向指针的引用而不是指向指针的指针是否有优势?

I have a bit of COM code that uses interface pointers. The original author of the code implemented functions that return an interface pointer like this:

HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject* &pEnumerator ); // (1)

instead of the traditional

HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject** ppEnumerator ); // (2)

The function (1) is called like this:

hRes = Query ( sQuery, pEnumerator ); // (3)

which definitely looks wrong but it works fine. I'm not sure if I'm just picking up this line because the out parameter is not a pointer to the output variable or because there's something wrong with this approach.

Is there an advantage to using a reference-to-pointer instead of a pointer-to-pointer for out parameters?

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

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

发布评论

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

评论(4

甲如呢乙后呢 2024-12-27 08:39:45

第一个示例是对指针的引用,即。对类型 IEnumWbemClassObject* 的引用:

HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject* &pEnumerator );

因此,如果 pEnumerator 被声明为 IEnumWbemClassObject* (我假设是这样),则您不需要不需要显式地将 pEnumerator 的地址传递给函数或取消引用函数内的变量来更改 pEnumerator 指向的位置(否则需要使用IEnumWbemClassObject** 的参数)。

对指针的引用与对任何其他类型的引用具有相同的行为,只需将上面的示例视为“对指针的引用”,而不是 < em>“指向引用的指针。” 不存在指向引用的指针这样的东西。

The first example is that of a reference to a pointer, ie. a reference to a type IEnumWbemClassObject*:

HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject* &pEnumerator );

Therefore if pEnumerator is declared as a IEnumWbemClassObject* (which I'm assuming it is), you don't need to explicitly pass the address of pEnumerator to the function or dereference the variable inside the function in order to change where pEnumerator points (which would otherwise be required with an argument of IEnumWbemClassObject**).

A reference to a pointer has the same behaviour as a reference to any other type, just think of the above example as being a "reference to a pointer" and not a "pointer to a reference." There's no such thing as a pointer to a reference.

云巢 2024-12-27 08:39:45

优点与任何使用引用而不是指针的优点相同:

  • 简单
  • 引用不能为空,因此在 Query 中分配给引用不会导致访问冲突

请注意,原始描述有错误: IEnumWbemClassObject* & 是对指针的引用,而不是对引用的指针。

The advantages are the same as any use of references instead of pointers:

  • simplicity
  • references can't be null, so assigning to a reference in Query won't cause an access violation

Note the original description was in error: IEnumWbemClassObject* & is a reference to a pointer, not a pointer to a reference.

Oo萌小芽oO 2024-12-27 08:39:45

最好考虑一下类型& foo* 作为对指针的引用,而不是相反,因为它不再意味着您可以通过指针和其他此类破坏 C++ 的想法来修改引用。它还使该函数调用更容易相信,因为它就像通过引用传递其他任何内容一样,不需要取消引用或特殊符号。

It's better to think of Type& foo* as a reference to a pointer rather than the other way around, as it no longer implies that you can modify the reference through the pointer and other such C++-breaking ideas. It also makes that function call a little easier to believe in as it's just like passing anything else in by reference, no dereferencing or special symbols are required.

你怎么这么可爱啊 2024-12-27 08:39:45

这是因为指针和引用在正常的 C++ 实现中表示相同(但是这是实现细节,不是标准的一部分。而且它对指针的引用,而不是对引用的指针,根本不允许创建指向引用的指针。

Its because pointer and reference are represented identically in normal c++ implementation (this is implementation detail however, not part of the standart. also its reference to pointer, not pointer to reference, you are not allowed to create pointer to reference at all.

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