Gice

Technology and General Blog

The shared_ptr is 1 form of good ideas of C++ that has the shared possession of the item produced by the pointer. It shares the ownership of the object when storing the pointer into one more item, and the shared reference counter counts the amount of owners. The shared_ptr will increase the reference counter by a single just after copying the object and decreases the reference counter by a person soon after destroying the object. The memory owned by the object is reallocated if the item owned by the last shared_ptr is ruined, or the reset() purpose is termed to assign a further pointer for the object. When the shared_ptr does not individual any item, then it is known as an vacant shared pointer. Various takes advantage of of the shared_ptr have been shown in this tutorial.

Instance 1: Declare Shared Pointer in Various Strategies

Three different means to declare a shared pointer are proven in the following example. A course with a constructor and a public process have been declared in the code. The constructor will print a concept when a new shared item is developed with the constructor. The Display() process will print messages based on the contacting shared pointer item. In this article, the to start with shared pointer item has been created without contacting the constructor. The second shared pointer item has been made by contacting the constructor. The third shared pointer has made by assigning the 1st shared pointer. The Show() approach has been identified as 3 periods by using three shared pointer objects.

//Include necessary libraries

#include things like

#include

usingnamespacestd

//Outline the course
classMyClass
    public:
    //Declare the constructor
    MyClass()
        cout<<“The constructor is called.n
   
    //Declare a method to print text
    voidDisplay(string str)
   
        cout<<“The Display() method is called from the “<< str <<” pointer.n
   

intmain()

    //Initialize shared_ptr without calling constructor
    shared_ptr p1 = make_shared()
    p1->Exhibit(“to start with”)
    //Initialize shared_ptr by calling constructor
    shared_ptr p2 = shared_ptr(newMyClass)
    p2->Show(“2nd”)

    //Initialize shared_ptr by assignment
    shared_ptr p3 = p1
    p3->Display(“3rd”)
    return0

Output:

The subsequent output will seem soon after executing the higher than code. The constructor has known as at the time of next item creation only. So, the concept of the constructor has been printed only a single time:

Example 2: Print the Saved Shared Pointer Locale

The get() operate shared pointer is utilized to return the saved, shared pointer location. The pursuing case in point will print the place of the saved, shared pointers that are designed by the course and the function. Here, a class with a constructor has been defined to be utilised for generating a shared pointer. A functionality has been declared to produce a shared pointer and print the shared pointer area using the get() purpose. In this code, the initial shared pointer has been developed employing the class, the 2nd shared pointer has been designed applying the perform, and the third shared pointer has been created by assigning the to start with pointer.

//Include things like necessary libraries

#incorporate

#include things like

usingnamespacestd

//Outline the class
classMyClass

    public:
    //Declare the constructor
    MyClass()
        cout<<“The constructor is called.n
   

//Define function to initialize the pointer
voidInit_shared_ptr()

    shared_ptr p2 (newMyClass)
    cout<<p2.get() <<n

intmain()

    //Initialize shared_ptr by calling constructor
    shared_ptr p1 = shared_ptr(newMyClass)
    cout<<p1.get() <<n

    //Initialize shared_ptr by calling function
    Init_shared_ptr()

    //Initialize shared_ptr by assignment
    shared_ptr p3 = p1
    cout<<p3.get() <<n
    return0

Output:

The following similar output will appear after executing the above code. In the output, the returned value of the get() function for the first and third shared pointers are the same. However, the second shared pointer is different:

Example 3: Count the Shared Pointer Objects

The following example depicts a way to count the number of objects pointed by a shared pointer after creating and destroying the pointer. A class with a constructor has been declared in the code. The first shared pointer has been created using the class, and the second shared pointer has been created using the first shared pointer. The number of objects pointed by both shared pointers before and after calling the reset() function has been printed later.

//Include necessary libraries

#include

#contain

usingnamespacestd
//Determine the class
classMyClass
    community:
    //Declare the constructor
    MyClass()
        cout<<“The constructor is called.n
   

intmain()

    //Initialize the first shared_ptr by calling constructor
    shared_ptr p1(newMyClass)

    //Display the number of shared_ptr objects by the first pointer
    cout<<“p1 pointing to “<< p1.use_count() <<” object(s).n
    //Initialize the second shared_ptr using the first shared_ptr
    shared_ptr p2(p1)
    //Display the number of shared_ptr objects by the first and second pointers
    cout<<“p2 pointing to “<< p2.use_count() <<” object(s).n
    cout<<“p1 pointing to “<< p1.use_count() <<” object(s).n
    //Remove the ownership of the first pointer from the shared_ptr object
    p1.reset()
    //Display the number of shared_ptr objects by the second pointer
    cout<<“p2 pointing to “<< p2.use_count() <<” object(s).n
    return0

Output:

The following output will appear after executing the above code. The first pointer, p1, is pointing to one object after creation. After creating the second pointer, p2, using the first pointer, p1, both pointers are pointing to two objects for sharing the pointer. After calling the reset() function for the pointer, p1, one object has been destroyed, and the pointer, p2, is now pointing to one object only.

Conclusion:

The purposes of using a shared pointer in C++ have been explained in this tutorial by using simple examples. Creating shared pointers in different ways, getting stored shared pointer location, and counting the number of objects pointed by the shared pointers. I hope the C++ coders will be able to use the shared pointer in their code after reading this tutorial.

Leave a Reply

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