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
C++ Smart Pointers
course content

Contenido del Curso

C++ Smart Pointers

C++ Smart Pointers

1. Introduction to Smart Pointers
2. Unique Pointers
3. Shared Pointers
4. Weak Pointers
5. References
6. Advanced topics

book
Creating 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.

Tarea
test

Swipe to show code editor

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.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 2
toggle bottom row

book
Creating 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.

Tarea
test

Swipe to show code editor

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.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 2
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt