QueryInterface basic question-- why the double pointer? 
Author Message
 QueryInterface basic question-- why the double pointer?

I'm just curious as to why QueryInterface needs a pointer to a pointer.  In
other words, why do something like this (pseudocode):

QueryInterface(iid, (void**)&pInterface)

{

    if (iid == IID_Whatever)
    {

            *pInterface = (IID_Whatever *)this;

    }

Quote:
}

What's wrong with just implementing things with a (void*) , and code things
up so it reads:

 pInterface = (IID_Whatever *)this;

I'm sure there's a good reason for the double pointer, but I'm missing it.



Tue, 28 Oct 2003 01:27:16 GMT  
 QueryInterface basic question-- why the double pointer?
That's basic C. Say you have

void f(int a)
{
    a = 1;

Quote:
}

int a = 0;
f(a);

The value of a stays 0, because any change to parameters is not visible
outside the function. Parameters in C get passed by value. To modify the
parameter, you'd need

void f(int *pa)
{
    *pa = 1;

Quote:
}

int a = 0;
f(&a);

So to return int, you need to pass int*. To return void*, you need to pass
void**.
--
With best wishes,
    Igor Tandetnik


Quote:
> I'm just curious as to why QueryInterface needs a pointer to a pointer.
In
> other words, why do something like this (pseudocode):

> QueryInterface(iid, (void**)&pInterface)

> {

>     if (iid == IID_Whatever)
>     {

>             *pInterface = (IID_Whatever *)this;

>     }

> }

> What's wrong with just implementing things with a (void*) , and code
things
> up so it reads:

>  pInterface = (IID_Whatever *)this;

> I'm sure there's a good reason for the double pointer, but I'm missing it.



Tue, 28 Oct 2003 01:35:17 GMT  
 QueryInterface basic question-- why the double pointer?
Yes, you're missing a very important part.  The second parameter is an "out"
parameter, meaning that the caller passes in an "empty box", and the function
fills it before returning.  To use your suggestion, the caller would pass in an
interface pointer, but the function would only get a copy of the pointer value
as its argument.  It would not be able to modify that pointer, but would only be
modifying the local copy.  That modification would then be lost once the
parameter went out of scope.

In short, it is the difference between pass-by-value and pass-by-reference.
Prior to C++ reference variables, pass-by-reference had to be done with
pointers.



Tue, 28 Oct 2003 01:36:00 GMT  
 QueryInterface basic question-- why the double pointer?
Right-- I know about pass-by-reference and all that.  I was just confused by
the purpose of the interface pointer.  I brain-locked and didn't think that
we needed to modify the interface pointer itself, but rather was thinking we
needed to modify what it was pointing to.  Oops.  Thanks for clearing it up.



Quote:
> Yes, you're missing a very important part.  The second parameter is an
"out"
> parameter, meaning that the caller passes in an "empty box", and the
function
> fills it before returning.  To use your suggestion, the caller would pass
in an
> interface pointer, but the function would only get a copy of the pointer
value
> as its argument.  It would not be able to modify that pointer, but would
only be
> modifying the local copy.  That modification would then be lost once the
> parameter went out of scope.

> In short, it is the difference between pass-by-value and
pass-by-reference.
> Prior to C++ reference variables, pass-by-reference had to be done with
> pointers.



Tue, 28 Oct 2003 02:10:21 GMT  
 QueryInterface basic question-- why the double pointer?
Very good - you return a pointer, hence the double pointer.
Have you forgotten C?

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================


Quote:
> I'm just curious as to why QueryInterface needs a pointer to a pointer.
In
> other words, why do something like this (pseudocode):

> QueryInterface(iid, (void**)&pInterface)

> {

>     if (iid == IID_Whatever)
>     {

>             *pInterface = (IID_Whatever *)this;

>     }

> }

> What's wrong with just implementing things with a (void*) , and code
things
> up so it reads:

>  pInterface = (IID_Whatever *)this;

> I'm sure there's a good reason for the double pointer, but I'm missing it.



Tue, 28 Oct 2003 02:36:07 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. QueryInterface basic question-- why the double pointer?

2. Why use a double pointer?

3. Basic pointer casting question

4. basic question about pointers and local vars

5. REALLY basic question : pointers

6. double char pointer question

7. double pointer (or pointer to pointer)

8. why queryinterface failed?

9. why failed queryinterface?

10. Invalid pointer returned by QueryInterface

11. Why Wont This Compile Right--Pointer to Pointer to Pointer to Float

12. a really basic question how to intiallyze a double pointer to NULL...

 

 
Powered by phpBB® Forum Software