I have a structure
typedef struct myStruct_st
{
int a;
}myStruct;
It can be created using
myStruct * myStruct_new()
{
printf("Allocate\n");
return new myStruct;
}
And deleted using
static void myStruct_free(myStruct * ptr)
{
printf("Deallocate\n");
delete ptr;
}
I want the memory allocated for the structure freed automatically
To this end, I wrote a template
template <class T>
class scoped_del
{
public:
scoped_del(T * p, void (*mfree)(T *)) :
p_(p),
mfree_(mfree)
{
}
~scoped_del()
{
mfree_(p_);
}
private:
T * p_;
void (*mfree_)(T *);
};
And use it like that
int main()
{
myStruct * st = myStruct_new();
class scoped_del<myStruct> ptr_st(st, myStruct_free);
return 0;
}
How can I make it a more standard way using stl or boost?