#include #include #include using namespace std; //unique_ptr class student { public: int id ; student( int idP ) { id = idP ; cout << "Constructor called for ;" << id << endl ; } student( const student& obj1 ) { id = obj1.id ; cout << "Copy Constructor called for ;" << id << endl ; } ~student( ) { cout << "Destructor called for ;" << id << endl ; } }; void function1( shared_ptr ptr ) { cout << "Inside function Reference count:" << ptr.use_count() << endl ; } int main() { std::shared_ptr valuePtr( new student(1) ) ; cout << "Student id:" << (*valuePtr).id << endl ; // cout << "Before function Reference count:" << valuePtr.use_count() << endl ; function1( valuePtr ) ; cout << "Outside function Reference count:" << valuePtr.use_count() << endl ; //Using the make_unique function shared_ptr valuePtr1 = make_shared< student >(2) ; cout << "Student id:" << (*valuePtr1).id << endl ; //Reference countins is used so that the destructor is //called only once. shared_ptr valuePtr2 = valuePtr ; cout << "Reference count:" << valuePtr2.use_count() << endl ; function1( valuePtr ) ; }