Sometimes, you may need to convert a std::shared_ptr
into a std::unique_ptr
in your C++ code. This could be useful when you want to transfer ownership of a dynamically allocated object to a new scope or when you want to enforce the uniqueness of ownership.
To convert a std::shared_ptr
to a std::unique_ptr
, you can make use of the std::move
function, which is available in the C++ Standard Library.
Here’s an example of how you can convert a std::shared_ptr
to a std::unique_ptr
:
#include <memory>
int main() {
std::shared_ptr<int> sharedPtr = std::make_shared<int>(42);
// Convert sharedPtr to uniquePtr
std::unique_ptr<int> uniquePtr = std::move(sharedPtr);
// Unique ownership of the int object is now with uniquePtr
// The sharedPtr is no longer valid
return 0;
}
In the example above, we first create a std::shared_ptr
called sharedPtr
that points to an int
with a value of 42
.
Then, we convert sharedPtr
to a std::unique_ptr
called uniquePtr
using std::move
. This transfers ownership from sharedPtr
to uniquePtr
.
After the conversion, uniquePtr
will be the sole owner of the dynamically allocated int
object, while sharedPtr
becomes empty.
It is important to note that after the conversion, you should not use the sharedPtr
anymore as it no longer points to a valid object.
Converting a std::shared_ptr
to a std::unique_ptr
can be helpful in scenarios where you need to explicitly manage ownership and ensure that a particular object has a single owner at any given time.
#C++ #SmartPointers