我可以将带有参数的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 class不兼容。
请建议。
答案 0 :(得分:0)
基于参数的函数不能与std::bind
一起使用ref class方法绑定c ++类方法。下面的工作为我工作:
在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 class)
m_controller->Register({
[=] { OnFinish(bool val); /*A method of this class that will be executed from the controller*/ }
});