J
Juha Nieminen
I know this has been explained before, but I just can't find the post.
Also, I can't find any info on this in the C++ FAQ Lite.
In order to make a smart pointer class work with incomplete types,
some trickery is needed. I found one solution somewhere, which looks
like this:
template<typename T>
class GenericPtr
{
private:
typedef void(*DeleteFunctionType)(T* p);
DeleteFunctionType deleteFunction;
T* ptr;
static void doDelete(T* p) { delete p; }
public:
GenericPtr(T* p): ptr(p), deleteFunction(doDelete) {}
~GenericPtr() { deleteFunction(ptr); }
// other functions omitted
};
I just can't understand why this works. What's the difference between
executing 'delete' directly in the destructor and the destructor calling
a function, which executes the 'delete'? I would like to understand this
(because understanding the idea makes it easier to remember the technique).
Also, I can't find any info on this in the C++ FAQ Lite.
In order to make a smart pointer class work with incomplete types,
some trickery is needed. I found one solution somewhere, which looks
like this:
template<typename T>
class GenericPtr
{
private:
typedef void(*DeleteFunctionType)(T* p);
DeleteFunctionType deleteFunction;
T* ptr;
static void doDelete(T* p) { delete p; }
public:
GenericPtr(T* p): ptr(p), deleteFunction(doDelete) {}
~GenericPtr() { deleteFunction(ptr); }
// other functions omitted
};
I just can't understand why this works. What's the difference between
executing 'delete' directly in the destructor and the destructor calling
a function, which executes the 'delete'? I would like to understand this
(because understanding the idea makes it easier to remember the technique).