Thread: Function Pointer Fun

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Function Pointer Fun

    Code:
    template<typename T>
    class CUIButton : public CUIObject
    {
    public:
    	CUIButton ()
    	{
    		CUITask::AddToList( this );
    	}
    	void Activate(int index=0)
    	{
    		// RUN whatever the button does
    		if ( !Object || !Function )	// prevent calling a null
    			return;
    
    		if ( Animation.GetCurrentFrame() == 0 )
    			Animation.SetCurrentFrame(1);
    		else
    			Animation.SetCurrentFrame(0);
    		//(Object->*Function)();
    	}
    	void ChangeFunctionPointer( T * NewObject, void (T::*NewFunction)(void) )
    	{
    		Function = NewFunction;
    	}
    	void ( T::*Function )( void );
    	T * Object;
    }
    See the Animation.Get / Set lines?

    The (Object->*Function)() causes it to crash if I run those lines in it. This seems to be that Object isn't copying properly, which leads me to show you this:

    Code:
    class CTestTask : public ITask
    {
    public:
    
    	AUTO_SIZE;
    
    	bool Start();
    	void Update();
    	void Stop();
    	void Test();
    
    private:
    
    	CSprite MySprite;
    	CUIButton<CTestTask> MyButton;
    
    };
    Code:
    bool CTestTask::Start()
    {
    	MyButton.ChangeFunctionPointer( this, Test );
    
    	return true;
    }
    
    void CTestTask::Test()
    {
    	if ( MyButton.Animation.GetCurrentFrame() )
    		MyButton.Animation.SetCurrentFrame(0);
    	else
    		MyButton.Animation.SetCurrentFrame(1);
    }
    Why does the function pointer crash it?

    Edit:

    Code:
    void CTestTask::Test()
    {
    	if ( CUITask::Objects[0]->Animation.GetCurrentFrame() )
    		CUITask::Objects[0]->Animation.SetCurrentFrame(0);
    	else
    		CUITask::Objects[0]->Animation.SetCurrentFrame(1);
    }
    If I do that, then it works. Problem? I'd have to also store the index. But I don't think I need that, do I?
    Last edited by Trauts; 01-21-2004 at 05:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM