我试图让一个类函数采用另一个类未知函数。我希望这有任何意义。我想做的是......如果单击按钮1(或任何声明的按钮),它将执行输入到函数Event__MouseClicked的第一个参数中的任何函数(任何“无效函数”)。
bool MouseClicked( void ){ /**/ };
namespace XE
{
class Window
{
public:
const char* Title;
};
class Button
{
public:
void Event__MouseClicked( void( *Function )( void ) ) {
if( MouseClicked( ) )
( *Function )( );
}
};
};
class XorrWindow1 : public XE::Window
{
public:
XorrWindow1( void ) {
Initialize( );
}
protected:
~XorrWindow1( void );
private:
XE::Button Button1;
XE::Button Button2;
private:
// EVENT PROTOTYPES
void DoSomething( void );
void RandomFunction( void );
void Initialize( void )
{
// INITIALIZE CONTROLS
// AND OTHER STUFF BELOW
this->Title = "XorrWindow1";
// How can I resolve this problem?
this->Button1.Event__MouseClicked( &XorrWindow1::DoSomething );
this->Button2.Event__MouseClicked( &XorrWindow1::RandomFunction );
};
};
void XorrWindow1::DoSomething( void ) {
::MessageBoxA( NULL, this->Title, "Button1 clicked!", MB_OK );
};
void XorrWindow1::RandomFunction( void ) {
::MessageBoxA( NULL, this->Title, "Button2 clicked!", MB_OK );
};
错误是这样的:
'XE::Button::Event__MouseClicked' : cannot convert parameter 1 from 'void (__thiscall XorrWindow1::* )(void)' to 'void (__cdecl *)(void)'
我完全理解导致错误的原因。但我不知道如何修复它,因为它必须能够采用 Window1 类的任何未知功能。