Contenido del Curso
C++ Smart Pointers
C++ Smart Pointers
Creating Unique Pointers
Technically, there are two ways to create a unique pointer. However, one of those ways is widely discouraged by C++ experts. For the sake of learning, we will explore both ways.
The recommended way – using std::make_unique
The std::make_unique
, introduced in C++14, is a function that provides a safe and efficient way to create unique pointers. It's a great alternative to using the new/delete
and new[]/delete[]
operators, which can make C++ code more susceptible to memory-related issues.
makeunique
#include <iostream> #include <memory> #include <vector> int main() { // Here we use std::make_unique to create a unique pointer to a vector of integers. std::unique_ptr<std::vector<int>> uniqueVectorPtr = std::make_unique<std::vector<int>>(); }
In the above code, std::make_unqiue
allocates the dynamic resource and returns a unique pointer that owns it. When the unique pointer goes out of scope, the resource is automatically deallocated. Moreover, it’s designed to be exception-safe, which eradicates the chances of resource leaks due to exceptions.
Note
Don’t use
std::make_unique
when you are specifying a custom deleter for your unique pointer. Custom deleters are an advanced concept that we will cover later in this course.*
The frowned-upon way – Direct initialization
You can also create a unique pointer directly by initializing it with the result of the new
operator. However, this approach is discouraged, due to potential resource leaks in case an exception is thrown.
newUniquePtr
#include <iostream> #include <memory> #include <vector> int main() { // Insecure way to create a unique pointer for a vector of integers. std::unique_ptr<std::vector<int>> uniqueVectorPtr(new std::vector<int>()); }
The vector in the above code will still be automatically destroyed when the unique pointer goes out of scope. However, for maximum exception safety and better code practices, always prefer using std::make_unique
.
Managing custom objects with unique pointers
Unique pointers are not limited to primitive data types (like integers) or standard containers (like vectors). They can also be used to manage dynamically allocated custom objects.
customObjUniquePtr
#include <iostream> #include <memory> class CustomObject { public: //defining a constructor of our CustomObject which takes in two values CustomObject(int value1, int value2) : param1(value1), param2(value2) { std::cout << "CustomObject constructed with values: " << param1 << " and " << param2 << std::endl; } void performAction() { std::cout << "CustomObject is performing an action." << std::endl; } //The destructor of our CustomObject, which will be called when the associated unique pointer goes out of scope. ~CustomObject() { std::cout << "CustomObject destroyed with values: " << param1 << " and " << param2 << std::endl; } private: int param1; int param2; }; int main() { // Using std::make_unique to create a unique pointer for a CustomObject, passing required values to the constructor std::unique_ptr<CustomObject> customObjectPtr = std::make_unique<CustomObject>(42, 77); // Invoking a member function customObjectPtr->performAction(); //Destructor of the custom object will be called when the function ends return 0; }
In the above code, we are managing a custom object using a unique pointer. As soon as the object goes out of scope, the unique pointer calls its destructor to deallocate it. Read the code comments to know exactly what is happening!
¡Gracias por tus comentarios!