Created
April 24, 2019 02:12
-
-
Save escoffier/23b4341565a473a52c1d699e869404d5 to your computer and use it in GitHub Desktop.
class member function pointer
This file contains hidden or 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> | |
#include <functional> | |
using namespace std; | |
struct Foo | |
{ | |
Foo (int num):num_ (num) | |
{ | |
} | |
void print_add (int i) const | |
{ | |
std::cout << num_ + i << '\n'; | |
} | |
int num_; | |
}; | |
template < typename T, typename F > void | |
bar (T obj, F f, int i) | |
{ | |
(obj.*f) (i); | |
} | |
int | |
main () | |
{ | |
const Foo foo (314159); | |
std::function < int (Foo const &) > f_num = &Foo::num_; | |
std::cout << "num_: " << f_num (foo) << '\n'; | |
bar (foo, &Foo::print_add, 10); | |
foo.Foo::print_add (20); | |
cout << "Hello World"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment