2017-12-13 6 views
1

refクラスメソッドに引数を指定してC++クラスメソッドをバインドできますか?C++クラスメソッド(パラメータ付き)をrefクラスメソッドにバインド

using Finish = std::function<void(bool status)>; 
class Controller 
{ 
public: 
    Controller(Finish func); 
private: 
    Finish m_onFinish; 
    bool m_isFinished = false; 
} 

Controller::Controller(Finish func) 
    : m_onFinish(func) 
{  
}  
Controller::Execute() 
{ 
    ... 
    m_isFinished = true; 
    ... 
    m_onFinish(m_isFinished) 
    ... 
} 

public ref class MainPage sealed 
{ 
    public : 
     MainPage(); 
     void OnFinished(bool status); 
    private: 
    std::unique_ptr<Controller> m_controller; 
}  
MainPage() 
{ 
    auto finishCallBack = std::bind(&OnError, this, std::placeholders::_1); 
    m_controller = std::make_unique<Controller>(finishCallBack); 
} 

私はエラーの下に取得しています:

error C2664: 'Controller::Controller(const Controller &)': cannot convert argument 1 from 'std::_Binder<std::_Unforced,void (__cdecl *)(bool),MainPage ^,const std::_Ph<1> &>' to 'Finish' 

これは通常、純粋なC++で動作します。しかし、それはrefクラスで動作しないように見えます。

お勧めします。

+1

ではおそらく、λ(HTTPを使用してみてください://en.cppreferenceを。 com/w/cpp/language/lambda) 'std :: bind'ではなく? –

+0

引数で試しましたか? –

答えて

0

refクラスメソッドを使用してC++クラスメソッドをバインドする場合、引数ベース関数はstd::bindでは機能しません。仕事の下には周りの私の仕事:

をC++クラス

using Finish = std::function<void(bool status)>; 

std::vector<Finish> m_funcs; 

void Executor() 
{ 
    for(const auto& func : m_funcs) 
    { 
     // Trigger the function 
     func(true); 
    } 
} 

void Register(const std::vector<Finish>& funcs) 
{ 
    m_funcs = funcs 
} 

メインページ(UWP REFクラス)

m_controller->Register({ 
      [=] { OnFinish(bool val); /*A method of this class that will be executed from the controller*/ } 
}); 
関連する問題