CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #3
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: Calling non-static member functions from within a static member function.

    Quote Originally Posted by Jehjoa
    I guess this is because non-static functions are supposed to get the hidden 'this' pointer, but because WndMsgHandler is static it doesn't have the 'this' pointer. Correct?
    Correct.

    Quote Originally Posted by Jehjoa
    Does this then mean that non-static functions can't be called from static ones? That would mean changing many of my memberfunctions to statics...
    No, making the member function static wouldn't change anything, since they wouldn't have the this pointer too. And all the functions can be called from static functions, you just need to pass them the this pointer. Usually, the best way to to that is to pass the this pointer as the last parameter of CreateWindow, and then catch the WM_NCCREATE message and store the pointer you passed - which is available in the lpCreateParams member variable of the LPCREATESTRUCT structure - using SetWindowLong and GWL_USERDATA:

    Code:
    void MyClass::SomeFunction()
    {
    
       CreateWindow(...., GetModuleHandle(NULL), this);
    
    }
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
       MyClass *c = (MyClass*)GetWindowLong(hwnd, GWL_USERDATA);
    
       if (msg == WM_NCCREATE)
       {
    	  LPCREATESTRUCT cs = (LPCREATESTRUCT)lParam;
    	 c = (MyClass*)cs->lpCreateParams;
    	 SetWindowLong(hwnd, GWL_USERDATA, (LONG)c);
       }
    
       //now you can access all the functions.
    }
    Last edited by kkez; March 3rd, 2007 at 11:40 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured