Is it safe to remove the only shared_ptr reference to itself inside a method? Something like the following. If two objects, one of class A and the other of class B, points to each other via their pB_ and pA_. Suppose pB_ is the only reference to the object of class B. Then I call A::method() on the object of class A. Will there be any problem?
#include <iostream>
using std::shared_ptr
class B;
class A {
public:
void method() {
pB_->method();
}
shared_ptr<B> pB_;
};
class B {
public:
void method() {
pA_->pB_.reset();
// Is this OK? And is it safe if I don't do this?
some_other_data_ = 10;
}
shared_ptr<A> pA_;
int some_other_data_;
};