Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ Pointers to Functions

Pointers to Functions

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Will Sayin   USA  (17 posts)  Bio
Date Tue 22 May 2007 03:03 AM (UTC)
Message
I tried to think of a way to phrase my problem but realized that the only way I could was to first show you what I have done already:

I have programmed a console-mouse class:

class MOUSE
{
    class MOUSE
{
     public:
     MOUSE()
     {
          //sets all pointers to NULL, etc.
     }
     
     MOUSE update();
     MOUSE getLoc( short &x, short &y );
     bool isat( short x, short y );
     bool blClick();
     bool brClick();
     
     private:
     short mX; //mouse X location
     short mY; //mouse Y location
     bool lClick;
     bool rClick;
};



I now want to add "buttons" to the class, as in coordinates on the screen that when clicked, perform a certain function.


struct BUTTONS
{
     public:
     void setfunc( 
     private:
     short TLX, TLY, BRX, BRY; //Top Left X, Top Left Y, Bottom Left X, Bottom Left Y
     short defcolor, overcolor; //default color, color when the mouse is over it.
     void (*function);
     ....


This is the way I was thinking of doing it. Add a linked list of button structures. In
MOUSE update()
add a loop that transverses the linked list of buttons and checks to see if the mouse coordinates are within any of the button's coordinates. If it is, it does the function pointed to by the function pointer in the BUTTONS structure. This is where my problem arises, I have not been able to figure out how to set what the pointer points to through another function. (Since I know that sentence doesn't really make sense, let me explain) The way I would do this without using function pointers is as follows:


struct BUTTON
{
    unsigned int tlX, tlY, brX, brY;
}

struct MOUSE
{
    bool click()
    unsigned int X, Y;
}

void anotherFoo();

void foo()
{
    MOUSE mouse;
    BUTTON button;
    if( mouse.click() )
    {
        if( mouse.X > button.tlX && mouse.X < button.brX &&
            mouse.Y > button.tlY && mouse.Y < button.brY )
        {
             anotherFoo()
        }
    }
}



However, with function-pointers, I can do it like this:



struct BUTTON
{
    (*void)func;
    unsigned int tlX, tlY, brX, brY;
    BUTTON *pNext;
};

struct MOUSE
{
    unsigned int x, y;
    BUTTON buttons;
    ....  //all the functions to add/remove buttons from the linked list
    void update();
    bool click();
};

void foo()
{
    MOUSE mouse;
    while( 1 )
    {
        mouse.update();
    }
}

void MOUSE::update()
{
    //loop through button linked list and if the mouse is over any of the buttons, to the function pointed to by the button.
}



Now that may seem a lot more verbose than my previous example, but with a crapload of buttons, it requires much less code.

Now for my question (Finally!):

I want to create a function that will set the function in the button structure.
IE:

void setFunc( address-of-the-function )
{
    (*void)func = address-of-the-function;
}


What do I put in for address-of-the-function?
For each instance of a button I would want a different function, so I would need a function to set that function. If I wanted to use a normal int data-type, I would write:


void setInt( int number )
{
    buttonNum = number;
}


But I am not using an int, I am using a pointer to a function, so what would I put there?

Is this even possible?

Thanks for reading such a giant post,
Will

Will Sayin
Developer, Legends of the Darkstone
www.darkstonemud.com:5432
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Tue 22 May 2007 03:50 AM (UTC)
Message
The easiest, IMO, is to first define a typedef:

typedef double (*HighLevelCostFunction)(const PointPathNode & node,
                                        const Point2d & curPoint,
                                        const Point2d & goalPoint);


Now, you can use HighLevelCostFunction as a type meaning 'pointer to a function that takes one PPN& and two P2d& as arguments and returns a double'.

Then to use this thing, I have a method that looks like:

vector<Point2d> simplePointSearch(HighLevelCostFunction costFunc,
                              HighLevelCostFunction heurFunc);


Now I can do something like:

double cost      = costFunc(curNode, successor, goal_);


which calls the function.

In your case, you would just declare an instance variable of the function type and then assign the parameter instead of calling it.

It's all very straightforward once you have the typedef, which means you need to get your head around the somewhat arcane function pointer type syntax...

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Will Sayin   USA  (17 posts)  Bio
Date Reply #2 on Wed 23 May 2007 04:45 PM (UTC)
Message
OK. I think I have the general idea, thanks Ksilyan.

Will Sayin
Developer, Legends of the Darkstone
www.darkstonemud.com:5432
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,593 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.