I am reading about shared pointers and the book say that it is bad idea having multiple shared pointers point to the same memory because if one shared_ptr reference count decrease to 0, it will deallocate that memory, making the other shared_ptr pointing to garbage.
int *z = new int;
shared_ptr<int> bad1(z);
shared_ptr<int> bad2(z);
However, when the reference count for z becomes 0, wouldn't both shared_ptr know this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…