Implement OWN unique pointer in c++11:
source code of own unique pointer:
/* Program: Own unique pointer Author: Alpha Master Date: 26 September 2021 */ //Header File #include<iostream> //own unique pointer class template<class T> class myUnique { T* ptr; public: //default constructor myUnique(); //parameter constructor myUnique(T* p); //move constructor myUnique(myUnique<T>&& obj); //move assignment myUnique<T>& operator=(myUnique<T>&& obj); //copy contructor myUnique(const myUnique<T>& obj) = delete; //assignment operator myUnique<T>& operator = (const myUnique<T>& obj) = delete; //destructor ~myUnique(); //operator* T operator *(); //operator-> T* operator->(); //get() T* get(); //release() T* release(); //reset() void reset(T* p = nullptr); }; //default constructor template<class T> myUnique<T>::myUnique() { std::cout<<"Default cons"<<std::endl; ptr = nullptr; } //parameter constructor template<class T> myUnique<T>::myUnique(T* p) { std::cout<<"Par cons"<<std::endl; ptr = p; } //move constructor template<class T> myUnique<T>::myUnique(myUnique<T>&& obj) { std::cout<<"move cons"<<std::endl; ptr = obj.ptr; //reset the passing object obj.ptr = nullptr; } //move assignment template<class T> myUnique<T>& myUnique<T>::operator=(myUnique<T>&& obj) { std::cout<<"move assign"<<std::endl; //delete the object if there if(ptr) { delete ptr; ptr =nullptr; } //reset the passing object ptr = obj.ptr; obj.ptr = nullptr; return *this; } //destructor template<class T> myUnique<T>::~myUnique() { std::cout<<"destructor"<<std::endl; if(ptr) { delete ptr; ptr = nullptr; } } //operator * template<class T> T myUnique<T>::operator*() { std::cout<<"operator *"<<std::endl; return(*ptr); } //operator -> template<class T> T* myUnique<T>::operator ->() { std::cout<<"operator->"<<std::endl; return(ptr); } //get() template<class T> T* myUnique<T>::get() { return (ptr); } //release() template<class T> T* myUnique<T>::release() { T* res = ptr; ptr = nullptr; return (res); } //reset() template<class T> void myUnique<T>::reset(T* p) { if(ptr) delete ptr; ptr = p; } int main() { std::cout<<"Own unique pointer"<<std::endl; myUnique<int> up; myUnique<int> up1(new int(5)); myUnique<int> up2(std::move(up1)); myUnique<int> up3; //myUnique<int> up9 (up2);//error: use of deleted function //up3 = up2; //error: use of deleted function up3 = std::move(up2); std::cout<<*up3<<std::endl; myUnique<std::string> up4(new std::string{"digestcpp"}); std::cout<<"Unique pointer has -> operation:"<<up4->append(".com")<<std::endl; //Using get() member function int* pIptr= up3.get(); std::cout<<"Int value using get():"<<*pIptr<<std::endl; //release int* pInRe = up3.release(); std::cout<<"Int value using release(), release() (give up the ownership, without calling delete, now delete responsibility is NOT with unique_ptr):"<<*pInRe<<std::endl; delete pInRe; //reset //delete the object and reset to nullptr up3.reset(); return 0; }
Output:
Own unique pointer Default cons Par cons move cons Default cons move assign operator * 5 Par cons operator-> Unique pointer has -> operation:digestcpp.com Int value using get():5 Int value using release(), release() (give up the ownership, without calling delete, now delete responsibility is NOT with unique_ptr):5 destructor destructor destructor destructor destructor