Skip to main content
gamedev.net
🔒 Locked

Function Pointers

Started by Reactivore 4 replies 300+ views
Original Post
Reactivore
Reactivore
Howdy, folks. Just here to ask a simple question. I''m creating a class for a button in DX, and the way I would like to do it would be to pass in a defined function to operate the button. It looks something like this. typedef class BUTTON { void *Execute; inline void BUTTON(); } Button; inline void GUI::BUTTON::BUTTON(void *Func()) { Execute = Func; } Is this basically how you do it. How could I get the code to work. Thanks for the help.
DanJames
DanJames

Here''s an example of pointers to functions in C:

int somefunc() { printf("in somefunc"); }
int anotherfunc() { printf("in anotherfunc"); }

void main() {

int (*func)();

func = somefunc;
func();

func = anotherfunc;
func();

}

If, for example, somefunc and anotherfunc took one parameter, (an int for example), the function-pointer would be declared as:

int (*func)(int);

DeltaVee
DeltaVee
uh...... C++ right?

why dont you DERIVE a class from your base class

    

class Button
{

Button();
virtual ~Button()

virtual void Execut(void ) = 0;
};

class MyButton1 : public Button
{
MyButton1();
virtual ~MyButton1();

virtual void Execute(void ); // implement this function

};

class AnotherButton : public Button
{
AnotherButton();
~AnotherButton();

virtual void Execute(); // implement this function

}

// yada yada yada ......



If your going to use a language you might as well use its features instead of re-inventing them.

--------------------------
Carpe Diem
D.V.Carpe Die<FONT C
Reactivore
Reactivore
Hey, thanks, guys. Your advice has been invaluable to the development of my project. Everything in the area of function pointers is good to go!
Kylotan
Kylotan
quote:
Original post by DeltaVee

uh...... C++ right?

why dont you DERIVE a class from your base class



You might want 2 buttons to look different and yet do the same thing (toolbar and menu item, perhaps?). You might want there to be a totally different way of accessing the function too (macros, scripting?) You might want to change the functionality of a button at runtime (customisable interface?) So it makes sense to keep them seperate like this. I would do it the way Anonymous did though, which emphasises that Buttons only take a certain type of function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.