[comp.lang.c++] Deleting smart pointers

jgro@lia (Jeremy Grodberg) (04/12/91)

I have a class of smart pointers, and have most of what I want working,
but I'm stumped on one thing: deleting the objects they point to.

For a regular pointer, you do something like this:

int* foo = new int(0);
// do something
delete foo;

I'd like to mimic that with

SmartPointer foo = new Smart(0);
// do something
delete foo;


The only way I see to do that, though, is to declare a delete operator
for the class SmartPointer which assumes the void* passed to it is in
fact a pointer to a SmartPointer, and deletes the real pointer inside
the SmartPointer class.  This is really quite an abuse of the rules, and
I'd like a cleaner way.

Currently, I am resorting to an explicit function call to delete the object:

//Want to "delete foo", but can't use that syntax
foo.deleteElem();  //can't even call the function "delete", since its reserved

This is annoying because it adds an extra burden on the programmer to
remember which type of pointers they are dealing with.

Any suggestions?



-- 
Jeremy Grodberg      "Show me a new widget that's bug-free, and I'll show
jgro@lia.com         you something that's been through several releases."