unique_ptr is a smart pointer available in C++ 11.
Below are the points that we shall see in unique_ptr.
1. Points on unique_ptr
2. Syntax for creating an unique pointer:
3. How to create an objet using make_unique?
4. Example 1
5. Example 2
6. How to use make_unique()?
Points on unique_ptr:
1. unique_ptr as the same suggests, it is unique and cannot be shared.
2. It cannot ne copied to another unique_ptr by using assignement operator.
3. The owner ship can only be moved from one to another.
4. It means that, when an object ownership is transffered, the original owner will no longer owns the object,
5. We use “unique_ptr” when we want to limit the ownership of the objects. Thereby simplyfying the program logic.
Syntax for creating an unique pointer:
unique_ptr <MyClass> p (new MyClass);
How to create an objet using make_unique?
unique_ptr <MyClass> p = make_unique<MyClass>();
Now let us understand “unique_ptr” with the help of numerous examples:
Example 1: Simple Example:
#include<iostream>
#include <memory>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;
class MyClass
{
public:
MyClass()
{
cout<<"In constructor"<<endl;
}
~MyClass()
{
cout<<"In destructor"<<endl;
}
};
int main()
{
unique_ptr<MyClass> p (new MyClass);
return 0;
}
Output:
In constructor
In destructor
So what is so unique in unique_ptr?
Now add the below statement in main():
int main()
{
unique_ptr<MyClass> p (new MyClass);
unique_ptr<MyClass> p1 = p; // new statement added
return 0;
}
You will recieve an error as below:
Main.cpp:27:25: error: call to deleted constructor of 'unique_ptr<MyClass>'
unique_ptr<MyClass> p1 = p;
^ ~
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/bits/unique_ptr.h:388:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
1 error generated.
This is because, unique_ptr pointers are unique.
Example 2:
Now we create a function that will return “unique_ptr” created:
#include<iostream>
#include <memory>
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;
class MyClass
{
public:
MyClass()
{
cout<<"In constructor"<<endl;
}
~MyClass()
{
cout<<"In destructor"<<endl;
}
};
unique_ptr <MyClass> fun()
{
unique_ptr<MyClass> p (new MyClass);
return p;
}
int main()
{
cout<<"Entered main()"<<endl;
unique_ptr<MyClass> p2 = fun();
cout<<"Exited main()"<<endl;
return 0;
}
Output:
Entered main()
In constructor
Exited main()
In destructor
Now you can think what is the difference between “p2 = p” and “p2 = fun()”?
Both are same, but you an achieve this by using move semantics.
i.e unique_ptr<MyClass> p1 = move(p);
Now the ownership is transffered from pointer p to pointer p2.
Here the ownership is not sharedm but rather moved.
How to use make_unique()?
Instead of using new, you can use make_unique() to create a new object.
It is recommended to use “make_unique()” instead of new to create an object.
Now we shall modify the function “fun” as below:
unique_ptr <MyClass> fun()
{
unique_ptr<MyClass> p = make_unique <MyClass>();
return p;
}
All the O/P will remain same as we saw in previous example.