C++ own shared Pointer:
source code with example:
/* Program: Own Shared Pointer Author: Alpha Master Date: 16 Nov 2021 */ //Header File #include<iostream> template<class T> class MySharedPointer { T* mPtr; int* mCount; public: //Default Constructor MySharedPointer(T* t = nullptr); //Destructor ~MySharedPointer(); //Copy Constructor MySharedPointer(const MySharedPointer<T>& obj); //Assignment operator MySharedPointer<T>& operator = (const MySharedPointer<T>& obj); //Move Constructor MySharedPointer(MySharedPointer<T>&& obj); //Move assignment operator MySharedPointer<T>& operator = (MySharedPointer<T>&& obj); //operator* T operator * (); //operator -> T* operator ->(); //get T* get(); //count int getCount(){ return (*mCount);} //clean up void cleanUp(); }; template<class T> MySharedPointer<T>::MySharedPointer(T* t) { std::cout<<"In Constuctor"<<std::endl; mPtr = t; mCount = new int(0); if(mPtr != nullptr) { (*mCount)++; } //std::cout<<"count:"<<*mCount<<std::endl; } template<class T> MySharedPointer<T>::~MySharedPointer() { std::cout<<"In destrcutor"<<std::endl; if(mCount != nullptr) { cleanUp(); } } template<class T> void MySharedPointer<T>:: cleanUp() { (*mCount)--; if(mCount == 0) { delete mPtr; mCount = nullptr; delete mCount; mCount = nullptr; } } template<class T> MySharedPointer<T>::MySharedPointer(const MySharedPointer<T>& obj) { std::cout<<"copy"<<std::endl; mPtr = obj.mPtr; mCount = obj.mCount; (*mCount)++; } template<class T> MySharedPointer<T>& MySharedPointer<T>::operator = (const MySharedPointer<T>& obj) { std::cout<<"In assignment"<<*mCount<<std::endl; //existing object clean if(mCount != nullptr) { cleanUp(); } //assign new one mPtr = obj.mPtr; mCount = obj.mCount; (*mCount)++; } template<class T> MySharedPointer<T>::MySharedPointer(MySharedPointer<T>&& obj) { std::cout<<"In Move"<<std::endl; mPtr = obj.mPtr; mCount = obj.mCount; std::cout<<"count in move"<<*mCount<<std::endl; obj.mPtr = nullptr; obj.mCount = nullptr; } template<class T> MySharedPointer<T>& MySharedPointer<T>::operator = (MySharedPointer<T>&& obj) { std::cout<<"In move assignment"<<*mCount<<std::endl; //existing object clean if(mCount != nullptr) { cleanUp(); } //assign new one mPtr = obj.mPtr; mCount = obj.mCount; obj.mPtr = obj.mCount = nullptr; } template<class T> T MySharedPointer<T>::operator * () { return *mPtr; } template<class T> T* MySharedPointer<T>::operator ->() { return mPtr; } template<class T> T* MySharedPointer<T>::get() { return mPtr; } int main() { std::cout<<"Own Shared Pointer"<<std::endl; MySharedPointer<int> sh1; std::cout<<"get Count"<<sh1.getCount()<<std::endl; MySharedPointer<int> sh2(new int (5)); sh1 = sh2; std::cout<<"get Count"<<sh1.getCount()<<std::endl; MySharedPointer<int> sh3(sh1); std::cout<<"get Count"<<sh1.getCount()<<std::endl; //move MySharedPointer<int> sh4(std::move(sh1)); std::cout<<"get Count, moving expecting 3:"<<sh4.getCount()<<std::endl; //move assignment MySharedPointer<int> sh5; sh5 = std::move(sh4); std::cout<<"get Count, move assignment so expecting 3:"<<sh5.getCount()<<std::endl; std::cout<<"value:"<<*sh5<<std::endl; return 0; }
Output:
Own Shared Pointer In Constuctor get Count0 In Constuctor In assignment0 get Count2 copy get Count3 In Move count in move3 get Count, moving expecting 3:3 In Constuctor In move assignment0 get Count, move assignment so expecting 3:3 value:5 In destrcutor In destrcutor In destrcutor In destrcutor In destrcutor