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
course content

Kursinnhold

C++ Smart Pointers

C++ Smart Pointers

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

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.

h

creating_weak_pointer

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.

h

converting

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

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

cpp

solution

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 2
toggle bottom row

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.

h

creating_weak_pointer

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.

h

converting

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

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

cpp

solution

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 2
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt