Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating And Using Weak Pointers | Weak Pointers
course content

Conteúdo do Curso

C++ Smart Pointers

Creating And Using Weak PointersCreating And Using Weak Pointers

The existence of weak pointers is tied to shared pointers. Typically, you should only use weak pointers when you are already using shared pointers in your code.

Creating weak pointers

We usually create a weak pointer from an existing std::shared_ptr. This ensures that the weak pointer is observing an object that is already being managed by at-least one shared pointer.

In the above example, weakPtr is constructed from sharedPtr. The lifetime of weakPtr is tied to sharedPtr.

Using weak pointers

To use the object pointed to by a std::weak_ptr, you must first convert it to a std::shared_ptr using the lock() method. The lock() method checks if the object is still alive and, if so, returns a std::shared_ptr to it. If the object has been destroyed, it returns an empty std::shared_ptr.

Locking a weak pointer is necessary to ensure that the object doesn’t get destroyed while being used. For better understanding, think about what will happen when you try to access the object directly from the weak pointer, without calling lock().

Since a weak pointer doesn’t affect the object’s lifetime, the object can get destroyed while you are still trying to use it. By converting it into a shared pointer, you increase the reference count, and ensure that the object doesn’t die while in your use.

In the above example, we call the lock() function on a weak pointer to convert it into a temporary shared pointer. In the next line, before performing any other action, we check whether the shared pointer is alive or not.

Tarefa

Complete the following code that creates a weak pointer from a shared pointer, converts the weak pointer into a shared pointer, and then safely tries to access the value of the dynamic object. Use the code comments as your guide.

Tudo estava claro?

Seção 4. Capítulo 2
toggle bottom row
course content

Conteúdo do Curso

C++ Smart Pointers

Creating And Using Weak PointersCreating And Using Weak Pointers

The existence of weak pointers is tied to shared pointers. Typically, you should only use weak pointers when you are already using shared pointers in your code.

Creating weak pointers

We usually create a weak pointer from an existing std::shared_ptr. This ensures that the weak pointer is observing an object that is already being managed by at-least one shared pointer.

In the above example, weakPtr is constructed from sharedPtr. The lifetime of weakPtr is tied to sharedPtr.

Using weak pointers

To use the object pointed to by a std::weak_ptr, you must first convert it to a std::shared_ptr using the lock() method. The lock() method checks if the object is still alive and, if so, returns a std::shared_ptr to it. If the object has been destroyed, it returns an empty std::shared_ptr.

Locking a weak pointer is necessary to ensure that the object doesn’t get destroyed while being used. For better understanding, think about what will happen when you try to access the object directly from the weak pointer, without calling lock().

Since a weak pointer doesn’t affect the object’s lifetime, the object can get destroyed while you are still trying to use it. By converting it into a shared pointer, you increase the reference count, and ensure that the object doesn’t die while in your use.

In the above example, we call the lock() function on a weak pointer to convert it into a temporary shared pointer. In the next line, before performing any other action, we check whether the shared pointer is alive or not.

Tarefa

Complete the following code that creates a weak pointer from a shared pointer, converts the weak pointer into a shared pointer, and then safely tries to access the value of the dynamic object. Use the code comments as your guide.

Tudo estava claro?

Seção 4. Capítulo 2
toggle bottom row
some-alt