static this pointer

Started by
8 comments, last by doctorsixstring 21 years, 10 months ago
(VC++) Is it possible to declare a this pointer as static? In one of my projects, I have an object that needs to launch a modeless dialog box. In order to have the DLGPROC function within the object, I have to declare the function static. However, doing this makes it so that I cannot access any outside variables or functions from the DLGPROC, which is something I need to do. One idea I had would be to somehow declare the this pointer static, thus allowing me to refer to all the variables and functions in my object from the static DLGPROC function. I know I may be way off in my thinking, I would greatly appreciate any suggestions that I could use to remedy this situation. -Mike
Advertisement
No, the this pointer cannot be static. The this pointer - by definition is a pointer to an instance - and can therefore not be shared among all instances.

I think what you might be needing is the Singleton design pattern. I believe there is several tutorials on that here on Gamedev.net
Jacob Marner, M.Sc.Console Programmer, Deadline Games
As an alternative, you could also attach a copy of the this pointer to the window data. This can be achieved by using something like

SetWindowLong(hMyDialog, GWL_USERDATA, (LONG)this);

To retrieve the pointer of the object the Dialog belongs to, you can now just write

(MyDialogClass*)GetWindowLong(hMyDialog, GWL_USERDATA);

Please note however that this requires pointers to be only 32 bits wide.
See the last post in this thread for a summary of what you can do, and links.
---visit #directxdev on afternet <- not just for directx, despite the name
quote:Original post by Chappa
Please note however that this requires pointers to be only 32 bits wide.


GetWindowLongPtr/SetWindowLongPtr?
---visit #directxdev on afternet <- not just for directx, despite the name
I can''t seem to pass SetWindowLong() a pointer to an object. I create a pointer, then simply cast it to a LONG when I pass the variable into the function. But when I try to retrieve the variable later, it fails to give me a valid pointer. Any thoughts?

-Mike
When are you setting and when are you retrieving this pointer?
---visit #directxdev on afternet <- not just for directx, despite the name
It looks something like this:


      CMyClass* pClass = new CMyClass();dialog_hwnd = CreateDialog( hinstance, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, (DLGPROC)LoadDeviceProc );SetWindowLong(dialog_hwnd, DWL_USER, (LONG)pClass);  


Then, when I need to use the pointer (from within the DLGPROC) I do this:


  LRESULT CALLBACK DialogProc(HWND hdwnd, UINT msg, WPARAM wParam, LPARAM lParam){CMyClass* pClass = (CMyClass*)(GetWindowLong(hdwnd, DWL_USER));//....}  


[edited by - doctorsixstring on June 26, 2002 4:06:21 PM]
This explanation assumes you will be using the described dialog only once, and will have only one instance of it. Make your dialog class a Singleton, using one of the methods described above. Add two functions: A so-called static dialog proc, and your normal dialog proc. Upon creation of the dialog, initialize your static pointer (normally called something like ''instance''). Then, have your dialog proc be your static dialog proc. Within the static dialog proc, check for the validity of the instance pointer, and call the actual dialog proc (i.e. instance->DialogProc(...)).

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

While CreateWindow is executed, WndProc processes several messages (WM_CREATE, for example). Your code will set this pointer after those initial messages are processed. Read the link I posted again for solutions.
---visit #directxdev on afternet <- not just for directx, despite the name

This topic is closed to new replies.

Advertisement