Skip to content

Instantly share code, notes, and snippets.

@escoffier
Created April 24, 2019 02:12
Show Gist options
  • Save escoffier/23b4341565a473a52c1d699e869404d5 to your computer and use it in GitHub Desktop.
Save escoffier/23b4341565a473a52c1d699e869404d5 to your computer and use it in GitHub Desktop.
class member function pointer
#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