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?
The first example is that of a reference to a pointer, ie. a reference to a type
IEnumWbemClassObject*:Therefore if
pEnumeratoris declared as aIEnumWbemClassObject*(which I’m assuming it is), you don’t need to explicitly pass the address ofpEnumeratorto the function or dereference the variable inside the function in order to change wherepEnumeratorpoints (which would otherwise be required with an argument ofIEnumWbemClassObject**).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.