Why have pointers to pointers for parameters that are interfaces?

Started by
4 comments, last by jtmerchant 20 years, 3 months ago
Why does DirectX use pointers to pointers on their interfaces such as IDirect3DDevice9**? For example, when you are trying to call a function, such as IDirect3D9::CreateDevice, one of the parameters is a pointer to a pointer to your new device. Now this got me thinking, and I tested some things. I came to some interesting facts: A IDirect3DDevice9* seems to be acting not like a pointer, but the actual object. If you use this as a parameter to one of your own functions, and you initiate the device from inside the function, you're initiating a device to a copy of the programmer's object, leaving the real object as it is, just like any regular variable, such as an int. But if you use a IDirect3DDevice9**, it acts like, say, a pointer to an int, where you can change the variable inside the function and it will retain the value even after execution is out of the scope of the function. Why is this so?? Correct me if information is inaccurate. [edited by - jtmerchant on January 12, 2004 4:58:13 PM]
Advertisement
DirectX uses the system called COM under windows. This is a system where there is a particular layout in the memory of the way the code is called. This system is language independant and allows you to extend the functionality of your app using things like OLE. It also allows you to maintain backwards compatibility as DirectX has done with previous versions of DirectX.

James
Because DX will set ptr for you.

eg.

Your device is a actually a pointer - IDirect3DDevice9* pDevice.

To create device, call CreateDevice, DX will create a device and assign a ptr that you past (to modify a pointer not it''s value you need a pointer to that pointer).

You don''t explicitly call delete pDevice but you call Release() because somebody else may be using device

class IDirect3DDevice9 : public IUnknown;

IUnknown::Release()
{
--refCount;
if (!refCount)
delete this;
}

So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".
Argh...james beat me...
So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".
generally using pointer to a pointer is used for the memory managment either in Directx or any other C++ application.think of the pointer as (box) in memory where you are storing the variable or object.pointer to a pointer is a box that contains the no or index. of the box containing the variable or object.so the run time can change the place in which it is storing the variable or object through changing the pointer while the pointer to the pointer is unchanged.your application can access the variable or object through the pointer to pointer with no lost data or errors.but if you just use a pointer to variable or object and the run time changed the place where it is storing the variable you won''t be able to access it. this is the way in which the operating system does the memory managment.
I don't know precisely what goes on under the hood in DirectX. But here's a simple example of a use for a pointer-to-a-pointer that's fairly transparent: an array of arrays; a 2d array.

//Create arraydouble ** arry = new double * [100];for(int j=0; j<100; j++){     arry[j] = new double[20];}//Do stuff//Delete arrayfor(int j=0; j<100; j++){     delete [] arry[j];}delete [] arry;  


[edited by - Terranfury on January 12, 2004 6:05:12 PM]

This topic is closed to new replies.

Advertisement