Consider the following C function:
void giveme(char **p) { *p = "Hello, World!"; }
This gives you a string. If we passed the string as a return value, we would only use the-to-char pointer: char *giveme() , but since we use the out parameter, we must have a pointer to what we want to return. In this case, a pointer to a pointer to a char.
In one case, when you come across such code, COM , where the main function of QueryInterface is defined as :
HRESULT QueryInterface( [in] REFIID riid, [out] void **ppvObject );
In the following code, we want to somehow get IUnknown and want the pointer pWhatever point to the IWhatever interface of our object. Call QueryInterface as follows:
IUnknown *pUnk = getIUnknownFromWherever(); IWhatever *pWhatever = NULL; HRESULT hr; hr = pUnk->QueryInterface(IID_IWHATEVER, &pWhatever);
Since we want the QueryInterface to change the pointer , not the pointee (the object it points to), we need to point it to the pointer.
Now the whole point of C ++ links is to make life easier in some common scenarios when pointers are used in C. One of them is parameters. So, the previous two code fragments in C ++ could be:
What is it. The transfer point of a pointer as a link is basically the same as passing a parameter as a non-constant link - if the caller modifies it. Here we want the pointer (and not int or double) to change, so we pass a reference to the pointer.