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

bookCreating 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.
Opgave

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.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 2
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you show an example of how to use lock() with std::weak_ptr?

What happens if I try to access the object after all shared_ptrs are destroyed?

Can you explain the difference between std::shared_ptr and std::weak_ptr in more detail?

close

Awesome!

Completion rate improved to 5.56

bookCreating And Using Weak Pointers

Stryg for at vise menuen

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.
Opgave

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 2
single

single

some-alt