【发布时间】:2018-05-27 00:59:19
【问题描述】:
我可以将带有参数的 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 类。
请提出建议。
【问题讨论】:
-
也许尝试使用lambda 而不是
std::bind? -
你尝试过论证吗?
标签: c++-cx