unique_ptr
unique_ptr stores one pointer only. We can assign a different object by removing the current object from the pointer.
Example:
C++
// C++ program to demonstrate the working of unique_ptr // Here we are showing the unique_pointer is pointing to P1. // But, then we remove P1 and assign P2 so the pointer now // points to P2. #include <iostream> using namespace std; // Dynamic Memory management library #include <memory> class Rectangle { int length; int breadth; public : Rectangle( int l, int b) { length = l; breadth = b; } int area() { return length * breadth; } }; int main() { // --\/ Smart Pointer unique_ptr<Rectangle> P1( new Rectangle(10, 5)); cout << P1->area() << endl; // This'll print 50 // unique_ptr<Rectangle> P2(P1); unique_ptr<Rectangle> P2; P2 = move(P1); // This'll print 50 cout << P2->area() << endl; // cout<<P1->area()<<endl; return 0; } |
Output
50 50
Smart Pointers in C++
Prerequisite: Pointers in C++
Pointers are used for accessing the resources which are external to the program – like heap memory. So, for accessing the heap memory (if anything is created inside heap memory), pointers are used. When accessing any external resource we just use a copy of the resource. If we make any changes to it, we just change it in the copied version. But, if we use a pointer to the resource, we’ll be able to change the original resource.