lundi 25 avril 2016

Need of a weak_ptr in C++11

I was reading "The C++ Standard Library" book by Nicolai M. Josuttis for understanding weak pointers. The author has mentioned 2 reasons for the need to have a weak_ptr and I don't get the second reason. Can anyone provide a simple explanation along with example of the below reason (quoted from the book) -

"Another example occurs when you explicitly want to share but not own an object. Thus, you have the semantics that the lifetime of a reference to an object outlives the object it refers to. Here, shared_ptrs would never release the object, and ordinary pointers might not notice that the object they refer to is not valid anymore, which introduces the risk of accessing released data."

Aucun commentaire:

Enregistrer un commentaire