Sign in to follow this  

small problem with source code

This topic is 4228 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in the sample code bool d3d::InitD3D(HINSTANCE hInstance,int width, int height, bool windowed,D3DDEVTYPE deviceType,IDirect3DDevice9** device) whats with the double * before device????

Share this post


Link to post
Share on other sites
Quote:
Original post by vinb
I believe that indicates a pointer to a pointer.


o.o i guess that makes sense :P just dont really understand why we would need a pointer to a pointer, also if thats the case can there be a pointer to a pointer to a pointer ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Here, you use a pointer to pass some kind of return value in the functuion's arguments. something like
void getMousePos(int* x,int* y), except that x is not an int, but a pointer. I believe they do this because the COM standard (directX uses COM) use the return value of the methods to return error codes, so they can't just pass the pointer in the return value.

Share this post


Link to post
Share on other sites
Quote:
Original post by LordFallout
Quote:
Original post by vinb
I believe that indicates a pointer to a pointer.


o.o i guess that makes sense :P just dont really understand why we would need a pointer to a pointer, also if thats the case can there be a pointer to a pointer to a pointer ?


The function needs a pointer to a pointer because if fills *device with a pointer value. Consider the following code:

bool my_malloc(unsigned int size, my_struct *p)
{
p = (my_struct*)malloc(size);
if (!p) return false;
return true;
}

When the function exits, p is unmodified because you can't modify a parameter. You can modify the memody where p points, but not p itself. Now, redo it with a pointer to a pointer:

bool my_malloc(unsigned int size, my_struct **p)
{
*p = (my_struct*)malloc(size);
if (!*p) return false;
return true;
}

now, p is not modified, but *p (the memory poinetr by p) is.

d3d::InitD3D(), in your example, works in a similar way. device can't be modified, but *device can, and *device is initialized with a new IDirect3DDevice9*.

You typically use this kind of method in this way:

IDirect3DDevice9* my_device = NULL;
d3d.InitD3D(blah blah blah, &my_device);

This way, my_device will point to the new device object.

Triple pointers exists, but are very rare (and barely readable, so experienced coders tend to avoid them).

Regards,

Share this post


Link to post
Share on other sites

This topic is 4228 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this