I'm trying to make a forwarding call wrapper with std::bind() of an internal member function inside instance which has been created as a shared_ptr<>. Look like there's no chance.
In a nutshell:
std::shared_ptr<Parent> _instance(std::make_shared<Child>());
_instance->init();
_instance->proc();
class Parent : std::enable_shared_from_this<Parent>
{
protected:
std::vector<std::function<void()>> _vector;
public:
virtual void init() = 0;
virtual void proc() final
{
for (auto& f : _vector) {
f();
}
}
};
class Child : public Parent
{
protected:
std::string _s1;
int _i1;
public:
virtual void init() override
{
// XXX Won't compile
_vector.push_back(std::bind(&Child::bingo, shared_from_this()));
// BUG Looks bad in case when current "this" has been wrapped by shared_ptr<Parent>
_vector.push_back(std::bind(&Child::bingo, this));
// NOTE Lambda doesn't work here as well, because I need an access(in the ::bingo()) to the data members
}
void bingo()
{
std::cout << "Bingo!" << _s1 << _i1 << std::endl;
}
};
This is not a real-life example, in case if someone would like to offer a redesign as a solution ;)