+ 1
Weak pointer | lock()
Hi I know that weak pointer is used to remove circular dependency... It just stores reference without increasing reference count. Having said that, I have a question on below code. Why can't we call class method from weak_ptr object ? By locking it, are we creating shared_ptr reference out of weak_ptr ? If not, what is happening... If yes, then why count is not increased ? https://code.sololearn.com/cGjH7YZG6mzB/?ref=app
3 Respuestas
+ 4
a weak pointer is only valid during the life time of the object it hold the address, meaning that as soon as you called the destructor of the object, the weak pointer is marked as expired, and you cannot access the object wich doesn't exist anymore in memory ^^
+ 1
I guess that weak pointer in c++ are an object that wrap a real pointer (so the real pointer is deleted when there's no more reference to the original value hold at that memory address and a flag is set to let the expired method return true, even if there's no more real pointer)
0
Okay... This is just valid during actual object life and hence we cannot call it without varification... Hence may be lock is there...
But does it make shared_pointer of weak pointer referred object ? If not, why allowed to call methods? If yes, why count is not incremented