Thread: Static member functions as window and dialog procedures

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    179

    Question Static member functions as window and dialog procedures

    Hi there,

    I just solved (after searching online) a tricky problem with the concept mentioned in the thread title.

    I had this line in a window procedure (which I found out later was declared as static):

    Code:
    switch (wmId)
    {
    case IDM_ABOUT:
        DialogBoxW(hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, DemoApp::About);
        break;
    }
    The DemoApp::About parameter kept on throwing errors. The compiler stated that it could not be used as a type DLGPROC. When I declared the function as static in the class declaration header file it was fine.

    I'm wondering why this is the case? Thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    If a member function is not static then it takes a hidden parameter, a pointer to the object that the function is called upon. So for a non-static member function, the call
    Code:
      obj.func(1, 2);
    is turned into
    Code:
      func(&obj, 1, 2); // &obj is called 'this' in the member function
    Obviously that would screw up a window procedure.
    A static member function does not have that extra parameter and so is basically just a regular function.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    179
    Thanks very much again John

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  2. static member functions
    By fsp in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2005, 03:22 PM
  3. Member functions as child window procedures
    By filler_bunny in forum Windows Programming
    Replies: 28
    Last Post: 03-15-2004, 09:45 AM
  4. illegal references in static member functions
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 12-31-2002, 10:11 AM
  5. static member functions
    By DJ81 in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2002, 06:20 AM

Tags for this Thread