Skip to content

Instantly share code, notes, and snippets.

@MastodonHQ
Created August 19, 2012 02:59
Show Gist options
  • Save MastodonHQ/3391336 to your computer and use it in GitHub Desktop.
Save MastodonHQ/3391336 to your computer and use it in GitHub Desktop.
A class that has a member that takes a function pointer
#include <iostream>
using namespace std;
class test
{
public:
//printer is a regular function that takes a const char and prints it
void printer(const char * printme){
cout << printme << endl;
}
//funcptr is a function pointer with a signature that should match printer
void (test::*funcptr)(const char * printme);
//caller is a function that takes a const char and a callback
void caller(const char * printme, void (test::*funcptr)(const char * printex)){
cout << printme << endl;
((this->*funcptr))(printme);
}
};
int main(){
test m;
m.printer("Some text");
m.funcptr = &test::printer;
(m.*(m.funcptr))("Something");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment