I have a class foo
, with a member bar
of type std::shared_ptr<int>
:
class foo
{
std::shared_ptr<int> bar;
/*other stuff here*/
};
In that class I want to assign a new int
to bar
. But I can't write bar = new int();
as the pointer does not have a public assignment operator.
How should I do this? I could std::move
or std::swap
but neither of those seem right.
A null shared_ptr does serve the same purpose as a raw null pointer. It might indicate the non-availability of data. However, for the most part, there is no reason for a null shared_ptr to possess a control block or a managed nullptr .
What is the technical problem with std::shared_ptr::unique() that is the reason for its deprecation in C++17? this function is deprecated as of C++17 because use_count is only an approximation in multi-threaded environment.
std::shared_ptr. std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object.
bar = std::make_shared<int>();
is one way, especially if you like to retain the tractability of an assignment operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With