Skip to content

Instantly share code, notes, and snippets.

@cagbal
Last active September 11, 2018 12:47
Show Gist options
  • Select an option

  • Save cagbal/790d74fc9cac51795d579c6a92ce6d20 to your computer and use it in GitHub Desktop.

Select an option

Save cagbal/790d74fc9cac51795d579c6a92ce6d20 to your computer and use it in GitHub Desktop.
Just practicing, don't take it too serious: Amazingly, Suprisingly and Super Smart Pointer Practice
#include <iostream>
using namespace std;
template<class T>
class MySuperSmartPtr
{
public:
T* ptr;
MySuperSmartPtr(T* pointer=nullptr) : ptr(pointer)
{
}
MySuperSmartPtr(MySuperSmartPtr&& smart_ptr) : ptr(smart_ptr.ptr)
{
smart_ptr.ptr = nullptr;
}
~MySuperSmartPtr()
{
delete ptr;
}
T& operator=(MySuperSmartPtr&& sptr)
{
if (&sptr == this)
{
return *this;
}
delete ptr;
ptr = sptr.ptr;
sptr.ptr = nullptr;
return *this;
}
T& operator*()
{
return *ptr;
}
T* operator->()
{
return ptr;
}
};
int main()
{
MySuperSmartPtr<int> mssp(new int(3));
cout<<*mssp;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment