When I try to run this code I get bad_weak_ptr exception:
using namespace std;
class O {
public:
O() {}
};
class A : public std::enable_shared_from_this<A>, virtual public O {
public:
A() {}
};
class B : public std::enable_shared_from_this<B>, virtual public O {
public:
B() {}
void GetShared() {
shared_from_this();
}
};
class C : public A, public B {
public:
C() {}
};
int main()
{
std::shared_ptr<B> pt = std::make_shared<C>();
pt->GetShared();
}
I would like to create instance of C class but then return B shared_ptr because that is what other interface requires. Is there a way to fix this code ? I cannot seem to do that.