Converting `std::unique_ptr` to raw pointer

Using .get() method

The simplest way to obtain a raw pointer from a std::unique_ptr is by using the .get() method, which returns the underlying raw pointer.

std::unique_ptr<int> uniquePtr(new int(42));
int* rawPtr = uniquePtr.get();

In the above example, get() is called on the std::unique_ptr object uniquePtr, returning the raw pointer to the int object it manages. It’s important to note that ownership of the object is not transferred, and the std::unique_ptr will continue to manage the object’s lifetime.

Converting ownership using .release()

If you need to transfer ownership from a std::unique_ptr to a raw pointer, you can use the .release() method. This method releases the ownership of the managed object and returns the raw pointer without destroying the object.

std::unique_ptr<int> uniquePtr(new int(42));
int* rawPtr = uniquePtr.release();

In this case, the ownership of the dynamically allocated int object is transferred from uniquePtr to rawPtr. It’s crucial to note that you are responsible for freeing the memory manually using delete operator or equivalent when using .release().

Conversion using std::move()

Another way to achieve ownership transfer is by using std::move() function from the <utility> header. This function moves the ownership of a std::unique_ptr to another object or function.

std::unique_ptr<int> uniquePtr(new int(42));
int* rawPtr = std::move(uniquePtr);

By calling std::move() on uniquePtr, we transfer the ownership to rawPtr. Similar to the previous example, you will need to manually deallocate the memory.

Conclusion

Converting a std::unique_ptr to a raw pointer can be useful in certain situations. It’s important to know the implications and remember the responsibilities that come with it, such as memory management. Remember to use the appropriate method or technique that fits your specific use case.

#C++ #SmartPointers