Technology and General Blog

The intelligent ideas are applied to allocate the source dynamically. A lot of sorts of good ideas are applied in C++ for various needs, these kinds of as auto_ptr, exclusive_ptr, and shared_ptr. The car_ptr pointer is deprecated in the new version of C++. The unique_ptr is used in replacement of the auto_ptr. The object of this pointer can acquire ownership of the pointer. The item of this pointer owns the pointer uniquely, and no other pointer can issue to the object. The exceptional_ptr deletes the objects mechanically. This pointer manages these objects if the objects are wrecked, or the value of the item is changed or the reset() operate is named. The capabilities of the special_ptr and the utilizes of this pointer are mentioned in this tutorial.

Main Components:

Two most important elements of the unique_ptr item are provided under:

A. Saved Pointer:

It is used to handle the objects produced by a exclusive pointer. It is developed at the time of pointer development, and it can be modified in distinctive techniques.

B. Stored Deleter:

It requires the argument of the saved pointer variety that is used to delete the managed item. It is also produced at the time of pointer generation, and it can be modified by distinctive ways.

Case in point 1: Generate a Pointer Object of a Class With Constructor

The way to declare the exceptional pointer objects of a course and obtain the strategy of the class is by working with the objects as shown in the pursuing instance. A course with a constructor and a general public process has been declared in the code. The constructor has 3 arguments. The very first special pointer has been created by calling the constructor with a few argument values. The Final result() method is named by the pointer object that calculates the sum of three argument values of the constructor. Upcoming, the next one of a kind pointer item is designed without calling the constructor, and the very first pointer is moved to the 2nd pointer. The Outcome() technique is identified as by the 2nd pointer item.

//Include vital libraries


#include things like

usingnamespace std

//Determine the course
course Addition

    int selection1, selection2, selection3
    //Declare the constructor
    Addition(int a, int b, int c)
        variety1 = a
        variety2 = b
        number3 = c
    //Declare process to estimate the sum
    int Consequence()
        return amount1 + quantity2 + number3

int most important()

    //Declare the to start with pointer
    special_ptr pointer1(new Addition(45, 55, 30))

    cout<<“The result of sum using the first pointer :”<Result() <<n
    //Declare the second pointer
    unique_ptr pointer2
    //Move the first pointer to the second pointer
    pointer2 = move(pointer1)

    cout<<“The result of sum using the second pointer :”<Result() <<n


The following output will appear after executing the above code. The sum of 45, 55, and 30 is 130 that has been printed for both pointers.

Example 2: Create a Pointer Object of a Class With Constructor and Destructor

The way to declare a unique pointer object of a class with constructor and destructor has been shown in the following example. The class contains a constructor with one argument, a public method named Display() to the value of the class variable, and a destructor that will print a destroy message before destroying the object of the class. The Display() method is called after creating the unique pointer object in the code.

//Include necessary libraries



usingnamespace std

//Determine the course
course Purchaser

    string name

    //Declare the constructor
    Buyer(string n)
        title = n
        cout<<“The resource is allocated.n

    //Declare method to print the customer name
    void Display()
        cout<<“The name of the customer is: “<< name <<n

    //Declare the destructor
        cout<<“The resource is destroyed.n

int main()

    //Allocate the Resource object that is owned by the unique_ptr
    unique_ptruPointer new Customer(“Mir Abbas”)
    uPointer->Display screen()


The subsequent output will look soon after executing the over code:

Instance 3: Test the Pointer Right after Transferring the Ownership

The way to look at the ownership of the exceptional pointer has demonstrated in the subsequent case in point by producing two distinctive tips of a class. A class with two string variables and a general public method has been declared in the code. The Reserve_information() method of the class is referred to as right after making the initially exclusive pointer item of the class. Next, the 2nd special pointer item has been designed, and the to start with pointer has been moved to the next pointer that destroys the to start with pointer. The ownership of both equally tips should really be checked later on.

//Include things like necessary libraries



usingnamespace std

//Determine the class
course Ebook

    string title = “The C++ Programming Language”
    string author = “Bjarne Stroustrup”

    //Declare method to print reserve information
        cout<<“Book Name: “<< title <<n
        cout<<“Author Name: “<< author <<n

int main()

    //Declare the first pointer
    unique_ptr pointer1(new Book())

    //Declare the 2nd pointer
    distinctive_ptr pointer2
    //Go the initially pointer to the 2nd pointer
    pointer2 = transfer(pointer1)

    //Check the very first pointer
    if (static_cast(pointer1)) cout<<“The first poiner is not nulln
    elsecout<<“The first poiner is nulln
    //Check the second pointer
    if (static_cast(pointer2)) cout<<“The second poiner is not nulln
    elsecout<<“The second poiner is nulln



The following output will appear after executing the above code. According to the output, the ownership of the first pointer was removed, and the message, “The first pointer is null” has printed for the first pointer. The ownership of the second pointer exists, and the message, “The first pointer is not null” has printed for the second pointer:


The purposes of using a unique pointer in C++ programming have been described in this tutorial by using multiple examples. The ways to create a unique pointer, transfer the ownership of the pointer, and check the current ownership of the pointer have been explained here to help the readers to know the use of the unique pointer properly.

Leave a Reply

Your email address will not be published. Required fields are marked *