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

Swipe to show menu

book
Creating And Using Weak Pointers

Creating Weak Pointers

You should typically create a std::weak_ptr from an existing std::shared_ptr. This ensures that the weak pointer observes an object that is already managed by at least one shared pointer.

creating_weak_pointer.h

creating_weak_pointer.h

copy
12
std::shared_ptr<int> p_shared = std::make_shared<int>(42); std::weak_ptr<int> p_weak(sharedPtr);

The std::weak_ptr p_weak is constructed from an existing std::shared_ptr p_shared. This means p_weak does not increase the reference count of p_shared but instead observes the same managed object. The validity of p_weak depends on p_shared; if all std::shared_ptr instances managing the object are destroyed, p_weak expires and can no longer be used to access the object directly.

Using Weak Pointers

To access an object managed by a std::weak_ptr, you must first convert it to a std::shared_ptr using lock(). This method checks whether the object is still alive and returns a std::shared_ptr to it and otherwise, it returns an empty std::shared_ptr.

Locking a weak pointer prevents the object from being destroyed while in use. Since std::weak_ptr does not contribute to the reference count, directly accessing the object would risk using a destroyed instance. Converting it to a std::shared_ptr increases the reference count, ensuring the object remains valid during use.

converting.h

converting.h

copy
12345
auto locked_shared = p_weak.lock(); if (locked_shared) // The object is alive, and you can use lockedSharedPtr to access it. else // The object has been deallocated, and lockedSharedPtr is empty.
Task

Swipe to start coding

Complete the following code to create a std::weak_ptr from a std::shared_ptr, convert it back to a std::shared_ptr, and safely access the value of the managed object.

Solution

solution.cpp

solution.cpp

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 5.56

book
Creating And Using Weak Pointers

Creating Weak Pointers

You should typically create a std::weak_ptr from an existing std::shared_ptr. This ensures that the weak pointer observes an object that is already managed by at least one shared pointer.

creating_weak_pointer.h

creating_weak_pointer.h

copy
12
std::shared_ptr<int> p_shared = std::make_shared<int>(42); std::weak_ptr<int> p_weak(sharedPtr);

The std::weak_ptr p_weak is constructed from an existing std::shared_ptr p_shared. This means p_weak does not increase the reference count of p_shared but instead observes the same managed object. The validity of p_weak depends on p_shared; if all std::shared_ptr instances managing the object are destroyed, p_weak expires and can no longer be used to access the object directly.

Using Weak Pointers

To access an object managed by a std::weak_ptr, you must first convert it to a std::shared_ptr using lock(). This method checks whether the object is still alive and returns a std::shared_ptr to it and otherwise, it returns an empty std::shared_ptr.

Locking a weak pointer prevents the object from being destroyed while in use. Since std::weak_ptr does not contribute to the reference count, directly accessing the object would risk using a destroyed instance. Converting it to a std::shared_ptr increases the reference count, ensuring the object remains valid during use.

converting.h

converting.h

copy
12345
auto locked_shared = p_weak.lock(); if (locked_shared) // The object is alive, and you can use lockedSharedPtr to access it. else // The object has been deallocated, and lockedSharedPtr is empty.
Task

Swipe to start coding

Complete the following code to create a std::weak_ptr from a std::shared_ptr, convert it back to a std::shared_ptr, and safely access the value of the managed object.

Solution

solution.cpp

solution.cpp

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

close

Awesome!

Completion rate improved to 5.56

Swipe to show menu

some-alt