Function Pointers

This is a discussion on Function Pointers within the C++ Programming forums, part of the General Programming Boards category; This code is heavily simplified. Basically, it works if I do this line in CTestTask::Start(): MyButton.ChangeFunctionPointer( Func ); But not ...

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

    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);
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    >> 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

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Then go to
    www.boost.org
    and get the function library.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 03:22 PM

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