class B;
std::vector<std::shared_ptr<B>> b;
b.emplace_back(std::make_shared<B>(B()));
std::shared_ptr<B> ptr1 = b[0];
std::shared_ptr<B> ptr2 = b[0];
//somewhere in my code
int index = someindex;
if ( !b[index].get()->isAlive() )
{
if ( b[index].use_count() == 1 )
{
b.erase(b.begin()+index);
}
}
what i did here is have share_ptrs in one object, and then in the middle of program i constantly check if isAlive() will return true, and then if its false im just going to wait until other shared_ptr that manages it destroyed before i delete it.
is this only okay to do this? will this defeat its purpose? or is there any disadvantages?
@JLBorges hi, would you tell me why would i use std::weak_ptr here?
im just starting in smart pointers, and shared_ptr is the first one that i needed
at any rate, whether im using shared_ptr or not, i will need to wait the other user of b[index] to be finished before removing the object to the vector.
@JLBorges okay i read it thoroughly, but this one deletes it when the object it manages is destroyed by checking wp.expired() in the predicate function,
what im doing is the opposite, it should only delete it when no one is needed the managed object.