指针到指针与引用到指针的差异(C ++) [英] Difference between pointer-to-pointer vs reference-to-pointer (C++)

查看:157
本文介绍了指针到指针与引用到指针的差异(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

而不是传统的

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

函数(1)的调用方式如下:

The function (1) is called like this:

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

这肯定会看起来错误,我不知道如果我只是拿起这行,因为out参数不是指向输出变量的指针,或者这个方法有错误。

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.

对于out参数,使用引用指针而不是指针指针是否有优势?

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

推荐答案

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

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

因此,如果 pEnumerator code> IEnumWbemClassObject * (我假设它是),你不需要显式地传递 pEnumerator 的地址函数或取消引用函数内的变量,以改变 pEnumerator 点(否则使用 IEnumWbemClassObject ** )。

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**).

对指针的引用与任何其他类型的引用具有相同的行为,只是将上述示例视为指向指针指向引用的指针。 没有指向引用的指针。 / strong>

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.

这篇关于指针到指针与引用到指针的差异(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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