I was looking at some sample code the other day and saw something I didn't really understand. After looking over the code I found some comments that said pointer to member function.
When would you use a pointer to a class function?
Printable View
I was looking at some sample code the other day and saw something I didn't really understand. After looking over the code I found some comments that said pointer to member function.
When would you use a pointer to a class function?
I wouldn't. It's pretty much not needed with polymorphism. I have used static member functions across dll boundaries but they're really the same thing as global anyway.
data stored in lists is a classic use of pointers to member functions.
there are quite a few uses for pointers to functions. i use them in the games that i make for quick decision making
rather than
you can do this:Code:enum States { STATE1, STATE2, STATE3, ... };
switch(gameState)
{
case STATE1:
{
func1();
break;
}
case STATE2:
{
func2();
break;
}
case STATE3:
{
func3();
break;
}
.
.
.
.
}
much quicker, quite funky, and very handy! :DCode:enum States { STATE1, STATE2, STATE3, ... };
typedef int (MyClass::*FuncType)(void);
static FuncType funcs[NUM_STATES] =
{
&MainWindow::func1,
&MainWindow::func2,
&MainWindow::func3,
.
.
.
.
};
(this->*funcs[gameState])();
U.