-
Function Pointers
This code is heavily simplified.
Basically, it works if I do this line in CTestTask::Start():
MyButton.ChangeFunctionPointer( Func );
But not with
MyButton.ChangeFunctionPointer( Test );
because Test is really CUIButton::Test, and not the same type as Func.
Is there a way to make both of them work?
Code:
class CUIButton : public CUIObject
{
public:
void Activate(int index=0);
bool ConditionMet();
void ChangeFunctionPointer( void (*NewFunction)(void) );
void ( *Function )( void );
};
void CUIButton :: ChangeFunctionPointer( void (*NewFunction)(void) )
{
Function = NewFunction;
}
void CUIButton :: Activate(int index)
{
// RUN whatever the button does
Function();
}
Code:
#pragma once
#include "Engine.h"
class CTestTask : public ITask
{
public:
AUTO_SIZE;
bool Start();
void Update();
void Stop();
void Test();
private:
CSprite MySprite;
CUIButton MyButton;
};
void Func( void )
{
}
bool CTestTask::Start()
{
MyButton.ChangeFunctionPointer( Test );
MySprite.LoadBitmap("Images/TestPic2");
MyButton.Animation.LoadFrameSet(0,1,"Images/Testpic", 5);
MyButton.x = 500;
MyButton.y = 500;
return true;
}
void CTestTask::Test()
{
MyButton.Animation.ChangeCurrentFrame(1);
}
-
>> Is there a way to make both of them work?
Make CUIButton::Test() static.
Go ahead and read through this one time.
http://www.function-pointer.org
gg
-
Then go to
www.boost.org
and get the function library.