#pragma once
using namespace std;
#include <memory>
template <typename E>
class Link
{
private:
static unique_ptr<Link<E>> freelist; // Pointer to the freelist head
// How to release the memory if I use Link<E>*
public:
E element;
Link *next;
Link(const E &eleval, Link *nextval = nullptr)
: element(eleval)
, next(nextval) {}
Link(Link *nextval = nullptr) : next(nextval) {}
void *operator new(size_t)
{
if (freelist == nullptr) return ::new Link;
unique_ptr<Link<E>> temp = freelist;
freelist = freelist->next;
return temp.get();
}
void operator delete(void* ptr)
{
((Link<E>*)ptr)->next = freelist.get();
freelist = make_unique<Link<E>>(ptr);
}
};
template <typename E>
unique_ptr<Link<E>> Link<E>::freelist = nullptr;
I previously use Link* to store the address of freelist, but then I found that I couldn't release the memory of freelist. So I decided to use unique_ptr to store it since the memory will be automatically released.
However, I got into this problem:
void operator delete(void* ptr)
{
((Link<E>*)ptr)->next = freelist.get();
freelist = *reinterpret_cast<unique_ptr<Link<E>>*>(ptr);
}
In this function I was trying to convert void* to unique_ptr and vice versa, But I got this error when I compile the code:
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'Link *' (or there is no acceptable conversion)
Is there any way other than this to convert from raw pointer to smart pointer?