For educational purposes only, I am writing a small class of smart pointers, not currently general, just as simple as unique_ptr in C ++ 11. What I want is not a fully working implementation, but just a base, create, default / custom delete, etc.
I am trying to look at the implementation of the standard inside the Microsoft visual studio, and I caught the general implementation, but I am stuck with default / user deletion. So my question is: what is the best way to implement such a function?
Can you easily implement it only for educational purposes, or in the end is it something too complicated and therefore not worth it?
amuses
Hi Ami,
what do you think of something like that?
template <class _Ty>
struct default_delete
{
constexpr default_delete() = default;
void operator()(_Ty* Ptr)
{
std::cout << "Default Delete" << std::endl;
}
};
template <class T, class _Dl=default_delete<T>>
class Smart_Pointer2_Base;
template <class T, class _Dl>
class Smart_Pointer2_Base
{
T *ptr;
_Dl _deleter;
public:
explicit Smart_Pointer2_Base(T *p = NULL) { ptr = p; }
Smart_Pointer2_Base(T* p, _Dl) { prt = p; _deleter = _Dl; }
~Smart_Pointer2_Base() { _deleter(ptr);}
T & operator * () { return *ptr; }
T * operator -> () { return ptr; }
};
int main()
{
struct CloserStruct {
void operator()(int* toDelete) { std::cout << "Custom Delete"<<std::endl; }
};
smtpr::Smart_Pointer2_Base<int, CloserStruct> pi(new int(5));
return 0;
}