You could use std::bind
and std::ref
, or their boost equivalent (but since you are using C++11, you may want to use standard classes and functions). So given this class:
#include <iostream>
struct MyClass
{
int x;
MyClass(int x_) : x(x_) { }
void some_func()
{
std::cout << x << std::endl;
}
};
You could pass the pointer on which the member function is to be invoked wrapped in an std::reference_wrapper
. Also, avoid using new
(and delete
!) and prefer using smart pointers for modeling object ownership:
#include <functional>
#include <memory>
int main(int argc, char *argv[])
{
std::shared_ptr<MyClass> pmc;
auto mfp = std::bind(&MyClass::some_func, std::ref(pmc));
pmc = std::make_shared<MyClass>(42);
mfp();
pmc = std::make_shared<MyClass>(1729);
mfp();
}
Here is a live example.