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

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
std::shared_ptr<int> p_shared = std::make_shared<int>(42);
std::weak_ptr<int> p_weak(sharedPtr);
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
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.
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.
Uppgift

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

#include <iostream>
#include <memory>
#include <string>

int main()
{
std::shared_ptr<std::string> p_shared_string = std::make_shared<std::string>("Hello, World!");

// Create a weak pointer from the shared pointer
std::weak_ptr<std::string> p_weak_string(p_shared_string);

// Convert the weak pointer to a shared pointer to access the string
std::shared_ptr<std::string> p_another_shared_string = p_weak_string.lock();

// Check if the shared pointer is valid before accessing the string
if (p_another_shared_string)
// Print the value of the string
std::cout << *p_another_shared_string << std::endl;
else
std::cout << "The string has already been deleted." << std::endl;
}
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 2
#include <iostream>
#include <memory>
#include <string>

int main()
{
// Assuming you have a shared pointer to a string
std::shared_ptr<std::string> p_shared_string = std::make_shared<std::string>("Hello, World!");

// Create a weak pointer from the shared pointer
___::___<std::string> p_weak_string = ___ ;

// Convert the weak pointer to a shared pointer to access the string
std::___<std::string> p_another_shared_string = ___;

// Check if the shared pointer is valid before accessing the string
if (___)
// Print the value of the string
std::cout << *___ << std::endl;
else
std::cout << "The string has already been deleted." << std::endl;
}

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt