Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference to a function

Status
Not open for further replies.

fugigoose

Programmer
Jun 20, 2001
241
US
I have a class. This class is to call upon a function that i specify. How do I make callback function pointer reference thingy? So I can 'set' what function the class calls. Btw this function is defined outside of the class and is not part of the class. Thank you.
 
You'd use it like this.

int func1() {return 1;}
int func2() {return 2;}

int main()
{
int (*FuncPtr)() = func1;

cout << FuncPtr() << endl;
FuncPtr = func2;
cout << FuncPtr() << endl;

return 0;
}
 
If you want, and if it's convenient, you can change the class to a class template based on the type of the "function." That way, you can use things like function objects as well. You'd never need to write the type of the calback, and you'd be able to use more than one type.

Alternately, you could go generic the other way and use something out of Boost.Function (
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top