I have a base class which derives from boost::enable_shared_from_this, and then another class which derives from both the base class and boost::enable_shared_from_this:
Here's how I would solve your problem:
#include
#include
using namespace boost;
class virt_enable_shared_from_this :
public enable_shared_from_this
{
public:
virtual ~virt_enable_shared_from_this() {}
};
template
class my_enable_shared_from_this : virtual public virt_enable_shared_from_this
{
public:
shared_ptr shared_from_this() {
return dynamic_pointer_cast(virt_enable_shared_from_this::shared_from_this());
}
};
class A : public my_enable_shared_from_this { };
class B : public my_enable_shared_from_this { };
class C : public A, public B, public my_enable_shared_from_this {
public:
using my_enable_shared_from_this::shared_from_this;
};
int main() {
shared_ptr c = shared_ptr(new C());
shared_ptr c_ = c->shared_from_this();
return 0;
}
This is painful and at least a bit ugly. But it works reasonably well, after a fashion. I think Fraser's idea of re-thinking your design is likely the better option.