Function Pointer Fun

This is a discussion on Function Pointer Fun within the C++ Programming forums, part of the General Programming Boards category; Code: template<typename T> class CUIButton : public CUIObject { public: CUIButton () { CUITask::AddToList( this ); } void Activate(int index=0) ...

  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, 05: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, 09:18 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  4. Replies: 7
    Last Post: 07-04-2007, 01:46 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21