On the following example below, something is escaping me. Why does this generate the usual weak_ptr exception when using the shared_from_this, if there is an outstanding shared_ptr reference to the object which calls it ?
class A : std::enable_shared_from_this<A> {
public:
static std::shared_ptr<A> create() {
return std::shared_ptr<A>(new A());
}
A() {}
void setParent(const std::shared_ptr<A>& other) {}
std::shared_ptr<A> keep() {
auto o = A::create();
o->setParent(shared_from_this());
return o;
}
};
int main() {
std::shared_ptr<A> a = A::create();
auto s = a->keep();
}