Created
August 19, 2012 02:59
-
-
Save MastodonHQ/3391336 to your computer and use it in GitHub Desktop.
A class that has a member that takes a function pointer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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