#include #include using namespace std ; class student { public: int id ; student( ) { id=0 ; } 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 ; } }; template < typename T > class SP { private: T* pData; // Generic pointer to be stored int* referenceCount ; public: SP(T* pValue) : pData(pValue) { referenceCount = new int ; *referenceCount = 1 ; } SP( const SP& anotherSP_object ) { cout << "Copy constructor getting called. " << endl ; pData = anotherSP_object.pData ; referenceCount = anotherSP_object.referenceCount ; //Updates it in all the copies *referenceCount = *referenceCount + 1 ; } ~SP() { (*referenceCount)-- ; cout << "SP Destructor for:" << pData->id << " referenceCount: " << *referenceCount << endl ; if ( *referenceCount == 0 ) { delete pData ; delete referenceCount ; } } T& operator* () { return *pData; } T* operator-> () { return pData; } }; int main() { student* ptr1 = new student(1) ; SP< student > SmartPointer1( ptr1 ); //Create another SP object to hold a new student with //id 2 //Create a vector to hold SP objects. // Push the 2 SP objects onto the vector //Observe the output . It should only show 2 destructors return 0 ; }