Skip to main content
gamedev.net
🔒 Locked

Dealing with LPDIRECTX* things

Started by Nacho Jul 31, 2002 at 10:21 PM 2 replies 350+ views
Original Post
Nacho
Nacho
I´m coding a 2D engine using DirectX 6.1. One of the classes is in charge of creating the several DD Objects needed such as surfaces, clipper, palette, etc. Something very weird happened today when I was testing this class: one of its methods is int CVideoObjectsFactory::BuildDDObject(LPDIRECTDRAW4 lpNewObj); Basically I pass a pointer to an IDIRECTDRAW4 interface and inside the method it is validated: LPDIRECTDRAW lpDD = NULL; hDDRetVal = DirectDrawCreate(NULL,&lpDD,NULL); // Error handling. lpDD->QueryInterface(IID_IDirectDraw4,(LPVOID*)&lpNewObj); // More Error handling. lpDD->Release(); lpDD = NULL; iDDObject++; iObjectsBuilt++; return BUILT_SUCCESSFUL; As I was saying, I tried this method and the compiler flagged an unhandled exception one line of code after the one that makes the call to this method (which uses the just created DDraw object). The watch window revealed that the pointer I passed to the method was NULL (that is, it the BuildDDObject method returned and the pointer was NULL). All my problem got solved by adding a simple character to the function declaration and definition: ''&''. int CVideoObjectsFactory::BuildDDObject(LPDIRECTDRAW4 &lpNewObj); But I don´t understand why. I mean, isn´t LPDIRECTDRAW4 a pointer to the IDIRECTDRAW4 interface? And if I pass a pointer to a function, doesn´t its value change? Can somebody explain me what happened here? Thanks in advance!!
niyaw
niyaw
quote:
Original post by Ignacio Liverotti
But I don´t understand why. I mean, isn´t LPDIRECTDRAW4 a pointer to the IDIRECTDRAW4 interface? And if I pass a pointer to a function, doesn´t its value change? Can somebody explain me what happened here? Thanks in advance!!

the function modifies the local copy of the pointer. if you want to modify the value of the pointer in the calling function as well, you need to pass the parameter by reference or use a pointer. read up on by-reference parameter passing in your favorite c++ book.

---
Come to #directxdev IRC channel on AfterNET
Nacho
Nacho
quote:
Original post by niyaw
[quote]Original post by Ignacio Liverotti
But I don´t understand why. I mean, isn´t LPDIRECTDRAW4 a pointer to the IDIRECTDRAW4 interface? And if I pass a pointer to a function, doesn´t its value change? Can somebody explain me what happened here? Thanks in advance!!

the function modifies the local copy of the pointer. if you want to modify the value of the pointer in the calling function as well, you need to pass the parameter by reference or use a pointer. read up on by-reference parameter passing in your favorite c++ book.

---
Come to #directxdev IRC channel on AfterNET

Yes, you´re right. I was pretty confused because, as I was passing a pointer to a function, I tought that it was going to get modified, when, in fact, the content of the adress the pointer is pointing to, is the thing that will be modified. Now I understand why passing a reference to a pointer works and something new about C, thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.