3

お気に入りのポインター問題を解決するために C++11 を使用しようとしています

LRESULT CALLBACK renderMan::WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
//some code
WNDPROC crazy = bind(&renderMan::WindowProc,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4);

エラー

1>renderman.cpp(50): error C2440: 'initializing' : cannot convert from 'std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>' to 'WNDPROC'
1>          with
1>          [
1>              _Forced=true,
1>              _Ret=LRESULT,
1>              _Fun=std::_Pmf_wrap<LRESULT (__cdecl glSurface::* )(HWND,UINT,WPARAM,LPARAM),LRESULT,glSurface,HWND,UINT,WPARAM,LPARAM,std::_Nil,std::_Nil,std::_Nil>,
1>              _V0_t=glSurface *const ,
1>              _V1_t=std::_Ph<1> &,
1>              _V2_t=std::_Ph<2> &,
1>              _V3_t=std::_Ph<3> &,
1>              _V4_t=std::_Ph<4> &,
1>              _V5_t=std::_Nil,
1>              <unnamed-symbol>=std::_Nil
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
4

4 に答える 4

0

解決策は、グローバル変数を使用することでした。そのようにして、コンストラクターはthis変数を のような別の変数にバインドしますthat。これはシングルトン パターンで機能しますが、私の最初の質問は未回答のままだと思います。

于 2013-07-13T20:49:18.830 に答える
0

bind の結果が関数オブジェクトであるのに対し、WNDPROC は関数ポインタです。コンパイラが言うように、WNDPROC に変換することはできません。

あなたができる:

auto crazy = bind(.....)
std::function<LRESULT CALLBACK(HWND, UINT, WPARAM, LPARAM)> crazy = bind(...)

しかし、それはあなたの問題を解決しないと思います。無料の機能がなければこれを行う方法はないと思います。多分このように:

std::function<LRESULT CALLBACK(HWND, UINT, WPARAM, LPARAM)> crazy;

LRESULT CALLBACK myWindowProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam )
{
    if(!crazy)
        return (LRESULT)nullptr;

    return crazy(hwnd,Msg,wParam,lParam)
}

//and then somewhere in your renderMan:
crazy = bind(&renderMan::WindowProc,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4);

// wherever you want:
SetWindowLongA( hwnd, GWL_WNDPROC, ( LONG )myWindowProc );
于 2013-06-29T17:55:24.103 に答える