我试图创建一个简单的windows api。
我需要一些帮助来设置side class中的函数指针 我试图将函数设置为类中的函数时遇到错误,无论如何这可以在不使用类中的静态的情况下完成吗?
class Window
{
public:
WIN_API Window();
struct WIN_API
{
bool(*OnMouseClick)(Vector2D position, MouseButton button);
bool(*OnCreate)();
}Event;
};
class MainWindow : public Window
{
public:
MainWindow() : Window()
{
//error here
this->Event.OnCreate = OnCreate;
this->Event.OnMouseClick = OnMouseClick;
}
bool OnCreate()
{
return true;
}
bool OnMouseClick(Vector2D position, MouseButton button)
{
return true;
}
};
答案 0 :(得分:0)
您必须绑定特定实例的成员函数。最好使用lambda和std::function
对象。
class Window
{
public:
Window();
struct WIN_API
{
std::function<bool(Vector2D, MouseButton)> OnMouseClick;
std::function<bool()> OnCreate;
} Event;
};
class MainWindow : public Window
{
public:
MainWindow()
{
this->Event.OnCreate =
[this] () { return this->OnCreate(); };
this->Event.OnMouseClick =
[this] (Vector2D position, MouseButton button)
{ return this->OnMouseClick(position, button); };
}
bool OnCreate()
{
return true;
}
bool OnMouseClick(Vector2D position, MouseButton button)
{
return true;
}
};