在已经定义了类实例的函数之后设置它

Sar*_*sen 1 c++ winapi

我试图给出一个类事件函数,如.onShow()和.onHide(),所以当显示对象时,.onShow()函数将运行.我怎样才能让函数像这样改变:

MyClass myInst = MyClass();
myInst.onShow = OnShowFunction;
Run Code Online (Sandbox Code Playgroud)

是否可以允许这个?

Ash*_*ain 6

You can use function pointers or std::function (also in BOOST) to achieve this. However, it looks like you might be trying to adopt a javascript or scripting-language programming style in C++ - I'd warn against that, you should change your programming style depending on how it fits the language. A typical alternative in C++ is to have onShow a virtual method that can be overriden by derivatives.