0

I am trying to use callback to create a window that is a member function which has access to class members:

class A
{
   public:
   A();
   LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
   
};

A::A()
{
    using namespace std::placeholders;
    auto callback = std::bind(&A::WndProc, this, _1);
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, callback, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("Example"), NULL };
}

and Im getting this error: enter image description here

error C2440: 'initializing': cannot convert from 'std::_Binder<std::_Unforced,LRESULT (__cdecl A::* )(HWND,UINT,WPARAM,LPARAM),A *,const std::_Ph<1> &>' to 'WNDPROC'

I do not understand what is wrong here, Im trying to do it from examples from similar questions, but I think I do not really understand what is happening here

Il'ya Zhenin
  • 1,272
  • 2
  • 20
  • 31
  • Apart from the image, add the text of the error as well. – user202729 Oct 02 '21 at 12:26
  • Also you may want to add link to the example that you're adapting from – user202729 Oct 02 '21 at 12:27
  • [c++ - How to std::bind a class function properly? - Stack Overflow](https://stackoverflow.com/questions/27514656/how-to-stdbind-a-class-function-properly): https://stackoverflow.com/a/27514711/5267751 – user202729 Oct 02 '21 at 12:27
  • @Ruks because it also need "this" parameter, when Im tryin to do that happens similar error: error C2440: 'initializing': cannot convert from 'LRESULT (__cdecl A::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC' – Il'ya Zhenin Oct 02 '21 at 12:47

1 Answers1

0

WNDCLASSEX is a struct from the WinAPI that accepts its callback as a WNDPROC which is a function pointer alias type.

Now, the return value of std::bind() isn't a function pointer object and isn't convertible to one either. You may want to look at this question: convert std::bind to function pointer for a workaround.

However, the straightforward solution to your problem is to not use std::bind() and to make WndProc() a static method instead:

class A
{
   public:
   A();
   static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
   
};

A::A()
{
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, &A::WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("Example"), NULL };
}
Ruks
  • 3,886
  • 1
  • 10
  • 22