Archived

This topic is now archived and is closed to further replies.

static this pointer

This topic is 5649 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

(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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites